Commit 98572905 authored by Havoc Pennington's avatar Havoc Pennington

2003-04-06 Havoc Pennington <hp@pobox.com>

	* test/Makefile.am: remove a lot of stuff that isn't immediately
	useful, it's in CVS history if we want it.

	* test/test-service.c: use dbus-mainloop instead of that
	watch.[hc] crack
parent df008ef9
2003-04-06 Havoc Pennington <hp@pobox.com>
* test/Makefile.am: remove a lot of stuff that isn't immediately
useful, it's in CVS history if we want it.
* test/test-service.c: use dbus-mainloop instead of that
watch.[hc] crack
2003-04-06 Havoc Pennington <hp@pobox.com>
* dbus/Makefile.am: split lists of sources into stuff that goes in
......
......@@ -2,7 +2,7 @@
INCLUDES=-I$(top_srcdir) $(DBUS_TEST_CFLAGS)
if DBUS_BUILD_TESTS
TEST_BINARIES=test-service echo-client echo-server unbase64 break-loader spawn-test test-segfault test-exit test-sleep-forever
TEST_BINARIES=test-service unbase64 break-loader spawn-test test-segfault test-exit test-sleep-forever
else
TEST_BINARIES=
endif
......@@ -15,31 +15,14 @@ endif
noinst_PROGRAMS= $(TEST_BINARIES) $(GCOV_BINARIES)
echo_client_SOURCES= \
echo-client.c \
watch.c \
watch.h
echo_server_SOURCES= \
echo-server.c \
watch.c \
watch.h
test_service_SOURCES= \
test-service.c \
watch.c \
watch.h
test-utils.c \
test-utils.h
unbase64_SOURCES= \
unbase64.c
# bus_test_SOURCES = \
# debug-thread.c \
# debug-thread.h \
# bus-test.c \
# bus-test-loop.c \
# bus-test-loop.h
break_loader_SOURCES= \
break-loader.c
......@@ -60,12 +43,9 @@ decode_gcov_SOURCES= \
TEST_LIBS=$(DBUS_TEST_LIBS) $(top_builddir)/dbus/libdbus-convenience.la
echo_client_LDADD=$(TEST_LIBS)
echo_server_LDADD=$(TEST_LIBS)
test_service_LDADD=$(TEST_LIBS)
unbase64_LDADD=$(TEST_LIBS)
break_loader_LDADD= $(TEST_LIBS)
#bus_test_LDADD=$(TEST_LIBS) $(top_builddir)/bus/libdbus-daemon.la
spawn_test_LDADD=$(TEST_LIBS)
decode_gcov_LDADD=$(TEST_LIBS)
......
#include "bus-test-loop.h"
#include <sys/time.h>
#include <stdio.h>
#define DBUS_COMPILATION /* cheat and use DBusList */
#include <dbus/dbus-list.h>
#undef DBUS_COMPILATION
typedef struct
{
long time;
DBusTimeout *timeout;
} LoopTimeout;
static DBusList *timeouts;
static long
get_time (void)
{
struct timeval r;
long time;
/* Can't use dbus-sysdeps here since that isn't
* available outside of libdbus.
*/
gettimeofday (&r, NULL);
time = r.tv_sec * 1000;
time += r.tv_usec / 1000;
return time;
}
static void
add_timeout (DBusTimeout *timeout,
void *data)
{
LoopTimeout *lt;
lt = dbus_new (LoopTimeout, 1);
lt->time = get_time () + dbus_timeout_get_interval (timeout);
lt->timeout = timeout;
_dbus_list_append (&timeouts, lt);
}
static void
remove_timeout (DBusTimeout *timeout,
void *data)
{
DBusList *link;
link = _dbus_list_get_first_link (&timeouts);
while (link != NULL)
{
LoopTimeout *lt = link->data;
if (lt->timeout == timeout)
{
_dbus_list_remove (&timeouts, lt);
return;
}
link = _dbus_list_get_next_link (&timeouts, link);
}
}
static dbus_bool_t running_loop;
void
bus_test_loop_quit (void)
{
running_loop = FALSE;
}
void
bus_test_loop_run (void)
{
running_loop = TRUE;
/* Horribly inefficient main loop */
while (running_loop)
{
DBusList *link, *list_copy;
long time;
time = get_time ();
_dbus_list_copy (&timeouts, &list_copy);
link = _dbus_list_get_first_link (&list_copy);
while (link != NULL)
{
LoopTimeout *lt = link->data;
if (lt->time <= time)
{
dbus_timeout_handle (lt->timeout);
_dbus_list_remove (&timeouts, lt);
}
link = _dbus_list_get_next_link (&list_copy, link);
}
}
}
void
bus_test_loop_hookup_with_server (DBusServer *server)
{
dbus_server_set_timeout_functions (server,
add_timeout, remove_timeout,
NULL, NULL);
}
void
bus_test_loop_hookup_with_connection (DBusConnection *connection)
{
dbus_connection_set_timeout_functions (connection,
add_timeout, remove_timeout,
NULL, NULL);
}
#include <dbus/dbus.h>
void bus_test_loop_hookup_with_server (DBusServer *server);
void bus_test_loop_hookup_with_connection (DBusConnection *connection);
void bus_test_loop_quit (void);
void bus_test_loop_run (void);
#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
#define DBUS_COMPILATION /* cheat and use DBusList */
#include <dbus/dbus-list.h>
#include <bus/connection.h>
#undef DBUS_COMPILATION
#include "debug-thread.h"
#include "bus-test-loop.h"
static DBusHandlerResult
message_handler (DBusMessageHandler *handler,
DBusConnection *connection,
DBusMessage *message,
void *user_data)
{
printf ("client got a message!: %s\n",
dbus_message_get_name (message));
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static void
new_connection_callback (DBusServer *server,
DBusConnection *new_connection,
void *data)
{
if (!bus_connection_setup (new_connection))
return;
bus_test_loop_hookup_with_connection (new_connection);
dbus_connection_ref (new_connection);
}
static void
die (const char *failure)
{
fprintf (stderr, "Unit test failed: %s\n", failure);
exit (1);
}
/* Here are the tests */
static dbus_bool_t test_hello_succeeding = TRUE;
static char *client1_name, *client2_name;
static int client1_stage = 0, client2_stage = 0;
#define TEST_HELLO_HANDLE_FAIL(x) do { if (!(x)) { printf ("failed at line %d\n", __LINE__); test_hello_succeeding = FALSE; goto out; } } while (0)
static DBusHandlerResult
test_hello_client1_handler (DBusMessageHandler *handler,
DBusConnection *connection,
DBusMessage *message,
void *user_data)
{
char *tmp = NULL;
if (!test_hello_succeeding)
goto out;
#if 1
printf ("In stage %d got message %s\n",
client1_stage, dbus_message_get_name (message));
#endif
if (dbus_message_name_is (message, DBUS_MESSAGE_HELLO))
{
TEST_HELLO_HANDLE_FAIL (client1_stage == 0);
TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &client1_name,
0));
client1_stage += 1;
}
else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
{
TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &tmp,
0));
#if 0
printf ("ServiceCreated is %s\n", tmp);
#endif
TEST_HELLO_HANDLE_FAIL (client1_stage == 1 || client1_stage == 3);
if (client1_stage == 1)
TEST_HELLO_HANDLE_FAIL (strcmp (client1_name, tmp) == 0);
else
TEST_HELLO_HANDLE_FAIL (strcmp (client2_name, tmp) == 0);
client1_stage += 1;
if (client1_stage == 4)
bus_test_loop_quit ();
}
else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_ACQUIRED))
{
TEST_HELLO_HANDLE_FAIL (client1_stage == 2);
TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &tmp,
0));
TEST_HELLO_HANDLE_FAIL (strcmp (client1_name, tmp) == 0);
client1_stage += 1;
}
else
{
printf ("client1 received unexpected message %s in stage %d\n",
dbus_message_get_name (message), client1_stage);
test_hello_succeeding = FALSE;
goto out;
}
out:
if (tmp)
dbus_free (tmp);
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static DBusHandlerResult
test_hello_client2_handler (DBusMessageHandler *handler,
DBusConnection *connection,
DBusMessage *message,
void *user_data)
{
char *tmp = NULL;
if (!test_hello_succeeding)
goto out;
if (dbus_message_name_is (message, DBUS_MESSAGE_HELLO))
{
TEST_HELLO_HANDLE_FAIL (client2_stage == 0);
TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &client2_name,
0));
client2_stage += 1;
}
else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
{
TEST_HELLO_HANDLE_FAIL (client2_stage == 1);
TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &tmp,
0));
TEST_HELLO_HANDLE_FAIL (strcmp (client2_name, tmp) == 0);
client2_stage += 1;
}
else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_ACQUIRED))
{
TEST_HELLO_HANDLE_FAIL (client2_stage == 2);
TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &tmp,
0));
TEST_HELLO_HANDLE_FAIL (strcmp (client2_name, tmp) == 0);
client2_stage += 1;
}
else
{
test_hello_succeeding = FALSE;
goto out;
}
out:
if (tmp)
dbus_free (tmp);
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static dbus_bool_t
test_hello_replies (void)
{
DBusConnection *connection;
DBusMessage *message;
DBusMessageHandler *handler;
DBusResultCode result;
/* First start client 1 */
connection = dbus_connection_open ("debug:name=test-server", &result);
bus_test_loop_hookup_with_connection (connection);
message = dbus_message_new (DBUS_SERVICE_DBUS,
DBUS_MESSAGE_HELLO);
handler = dbus_message_handler_new (test_hello_client1_handler, NULL, NULL);
dbus_connection_add_filter (connection, handler);
if (!dbus_connection_send (connection, message, NULL))
die ("no memory to send message");
dbus_message_unref (message);
/* Then start client 2 */
connection = dbus_connection_open ("debug:name=test-server", &result);
bus_test_loop_hookup_with_connection (connection);
message = dbus_message_new (DBUS_SERVICE_DBUS,
DBUS_MESSAGE_HELLO);
handler = dbus_message_handler_new (test_hello_client2_handler, NULL, NULL);
dbus_connection_add_filter (connection, handler);
if (!dbus_connection_send (connection, message, NULL))
die ("no memory to send message");
dbus_message_unref (message);
bus_test_loop_run ();
return test_hello_succeeding;
}
int
main (int argc,
char **argv)
{
DBusServer *server;
DBusResultCode result;
debug_threads_init ();
bus_connection_init ();
server = dbus_server_listen ("debug:name=test-server", &result);
dbus_server_set_new_connection_function (server,
new_connection_callback,
NULL, NULL);
bus_test_loop_hookup_with_server (server);
if (server == NULL)
{
fprintf (stderr, "Failed to start server: %s\n",
dbus_result_to_string (result));
return 1;
}
if (!test_hello_replies ())
die ("hello with replies");
printf ("all tests succeeded\n");
return 0;
}
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-test.c Program to run all tests
*
* Copyright (C) 2002 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
*
*/
#include <stdlib.h>
#include <dbus/dbus.h>
#include "debug-thread.h"
#define DBUS_COMPILATION
#include <dbus/dbus-internals.h>
#undef DBUS_COMPILATION
static DBusMutex * tmutex_new (void);
static void tmutex_free (DBusMutex *mutex);
static dbus_bool_t tmutex_lock (DBusMutex *mutex);
static dbus_bool_t tmutex_unlock (DBusMutex *mutex);
static DBusCondVar*tcondvar_new (void);
static void tcondvar_free (DBusCondVar *cond);
static void tcondvar_wait (DBusCondVar *cond,
DBusMutex *mutex);
static dbus_bool_t tcondvar_wait_timeout (DBusCondVar *cond,
DBusMutex *mutex,
int timeout_msec);
static void tcondvar_wake_one (DBusCondVar *cond);
static void tcondvar_wake_all (DBusCondVar *cond);
static const DBusThreadFunctions functions =
{
DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
tmutex_new,
tmutex_free,
tmutex_lock,
tmutex_unlock,
tcondvar_new,
tcondvar_free,
tcondvar_wait,
tcondvar_wait_timeout,
tcondvar_wake_one,
tcondvar_wake_all
};
static DBusMutex *
tmutex_new (void)
{
int *tmutex;
tmutex = malloc (sizeof (int*));
*tmutex = 0;
return (DBusMutex *)tmutex;
}
static void
tmutex_free (DBusMutex *mutex)
{
free (mutex);
}
static dbus_bool_t
tmutex_lock (DBusMutex *mutex)
{
int *tmutex = (int *)mutex;
_dbus_assert (*tmutex == 0);
*tmutex = 1;
return TRUE;
}
static dbus_bool_t
tmutex_unlock (DBusMutex *mutex)
{
int *tmutex = (int *)mutex;
_dbus_assert (*tmutex == 1);
*tmutex = 0;
return TRUE;
}
static DBusCondVar*
tcondvar_new (void)
{
return (DBusCondVar*)0xcafebabe;
}
static void
tcondvar_free (DBusCondVar *cond)
{
}
static void
tcondvar_wait (DBusCondVar *cond,
DBusMutex *mutex)
{
int *tmutex = (int *)mutex;
_dbus_assert (*tmutex == 1);
}
static dbus_bool_t
tcondvar_wait_timeout (DBusCondVar *cond,
DBusMutex *mutex,
int timeout_msec)
{
int *tmutex = (int *)mutex;
_dbus_assert (*tmutex == 1);
return TRUE;
}
static void
tcondvar_wake_one (DBusCondVar *cond)
{
}
static void
tcondvar_wake_all (DBusCondVar *cond)
{
}
void
debug_threads_init (void)
{
dbus_threads_init (&functions);
}
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-test.c Program to run all tests
*
* Copyright (C) 2002 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
*
*/
#ifndef DEBUG_THREAD_H
#define DEBUG_THREAD_H
void debug_threads_init (void);
#endif
#include <dbus/dbus.h>
#include <stdio.h>
#include "watch.h"
int
main (int argc,
char **argv)
{
DBusConnection *connection;
DBusError error;
DBusMessage *message;
if (argc < 2)
{
fprintf (stderr, "Give the server address as an argument\n");
return 1;
}
dbus_error_init (&error);
connection = dbus_connection_open (argv[1], &error);
if (connection == NULL)
{
fprintf (stderr, "Failed to open connection to %s: %s\n",
argv[1], error.message);
dbus_error_free (&error);
return 1;
}
setup_connection (connection);
/* Send a message to get things going */
message = dbus_message_new ("org.freedesktop.DBus.Test", "org.freedesktop.DBus.Test");
if (!dbus_connection_send (connection,
message,
NULL))
fprintf (stderr, "No memory to send reply\n");
dbus_message_unref (message);
do_mainloop ();
dbus_connection_unref (connection);
return 0;
}
#include <dbus/dbus.h>
#include <stdio.h>
#include "watch.h"
static void
new_connection_callback (DBusServer *server,
DBusConnection *new_connection,
void *data)
{
printf ("Got new connection\n");
dbus_connection_set_max_live_messages_size (new_connection,
10);
setup_connection (new_connection);
}
int
main (int argc,
char **argv)
{
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;
}
setup_server (server);
dbus_server_set_new_connection_function (server,
new_connection_callback,
NULL, NULL);
do_mainloop ();
dbus_server_disconnect (server);
dbus_server_unref (server);
return 0;
}
#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
#include "watch.h"
#include "test-utils.h"
static DBusLoop *loop;
static void
die (const char *message)
......@@ -79,7 +79,12 @@ main (int argc,
return 1;
}
setup_connection (connection);
loop = _dbus_loop_new ();
if