mirror of
https://gitlab.freedesktop.org/dbus/dbus.git
synced 2026-01-03 22:00:16 +01:00
Factor out some utility functions from test/dbus-daemon*
In the process, make test_kill_pid() safer: do not try to terminate more than one pid, or the NULL handle. Also stop leaking the address_fd in spawn_dbus_daemon, a pre-existing bug that was spotted by Philip Withnall during review. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=88810 Reviewed-by: Philip Withnall
This commit is contained in:
parent
6976a7f199
commit
8b238f3f0d
7 changed files with 330 additions and 350 deletions
|
|
@ -25,15 +25,23 @@ static_cppflags = \
|
|||
|
||||
noinst_LTLIBRARIES = libdbus-testutils-internal.la
|
||||
|
||||
libdbus_testutils_la_SOURCES = \
|
||||
test-utils.c \
|
||||
test-utils.h \
|
||||
$(NULL)
|
||||
|
||||
if DBUS_WITH_GLIB
|
||||
libdbus_testutils_la_SOURCES += \
|
||||
test-utils-glib.c \
|
||||
test-utils-glib.h \
|
||||
$(NULL)
|
||||
endif
|
||||
|
||||
# You can link either libdbus-testutils, dbus-glib and libdbus-1,
|
||||
# or libdbus-testutils-internal and libdbus-internal - never both in the
|
||||
# same binary.
|
||||
if DBUS_WITH_DBUS_GLIB
|
||||
noinst_LTLIBRARIES += libdbus-testutils.la
|
||||
libdbus_testutils_la_SOURCES = \
|
||||
test-utils.c \
|
||||
test-utils.h \
|
||||
$(NULL)
|
||||
libdbus_testutils_la_LIBADD = \
|
||||
$(top_builddir)/dbus/libdbus-1.la \
|
||||
$(GLIB_LIBS) \
|
||||
|
|
@ -50,8 +58,7 @@ libdbus_testutils_internal_la_CPPFLAGS = \
|
|||
$(static_cppflags) \
|
||||
$(NULL)
|
||||
libdbus_testutils_internal_la_SOURCES = \
|
||||
test-utils.c \
|
||||
test-utils.h \
|
||||
$(libdbus_testutils_la_SOURCES) \
|
||||
$(NULL)
|
||||
libdbus_testutils_internal_la_LIBADD = \
|
||||
$(top_builddir)/dbus/libdbus-internal.la \
|
||||
|
|
|
|||
|
|
@ -27,21 +27,9 @@
|
|||
|
||||
#include <config.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifdef DBUS_WIN
|
||||
# include <io.h>
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <signal.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "test-utils.h"
|
||||
#include "test-utils-glib.h"
|
||||
|
||||
#define SENDER_NAME "test.eavesdrop.sender"
|
||||
#define SENDER_PATH "/test/eavesdrop/sender"
|
||||
|
|
@ -91,99 +79,6 @@ typedef struct {
|
|||
dbus_bool_t politelistener_got_stopper;
|
||||
} Fixture;
|
||||
|
||||
#define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
|
||||
static void
|
||||
_assert_no_error (const DBusError *e,
|
||||
const char *file,
|
||||
int line)
|
||||
{
|
||||
if (G_UNLIKELY (dbus_error_is_set (e)))
|
||||
g_error ("%s:%d: expected success but got error: %s: %s",
|
||||
file, line, e->name, e->message);
|
||||
}
|
||||
|
||||
static gchar *
|
||||
spawn_dbus_daemon (gchar *binary,
|
||||
gchar *configuration,
|
||||
GPid *daemon_pid)
|
||||
{
|
||||
GError *error = NULL;
|
||||
GString *address;
|
||||
gint address_fd;
|
||||
gchar *argv[] = {
|
||||
binary,
|
||||
configuration,
|
||||
"--nofork",
|
||||
"--print-address=1", /* stdout */
|
||||
NULL
|
||||
};
|
||||
|
||||
g_spawn_async_with_pipes (NULL, /* working directory */
|
||||
argv,
|
||||
NULL, /* envp */
|
||||
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
|
||||
NULL, /* child_setup */
|
||||
NULL, /* user data */
|
||||
daemon_pid,
|
||||
NULL, /* child's stdin = /dev/null */
|
||||
&address_fd,
|
||||
NULL, /* child's stderr = our stderr */
|
||||
&error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
address = g_string_new (NULL);
|
||||
|
||||
/* polling until the dbus-daemon writes out its address is a bit stupid,
|
||||
* but at least it's simple, unlike dbus-launch... in principle we could
|
||||
* use select() here, but life's too short */
|
||||
while (1)
|
||||
{
|
||||
gssize bytes;
|
||||
gchar buf[4096];
|
||||
gchar *newline;
|
||||
|
||||
bytes = read (address_fd, buf, sizeof (buf));
|
||||
|
||||
if (bytes > 0)
|
||||
g_string_append_len (address, buf, bytes);
|
||||
|
||||
newline = strchr (address->str, '\n');
|
||||
|
||||
if (newline != NULL)
|
||||
{
|
||||
if ((newline > address->str) && ('\r' == newline[-1]))
|
||||
newline -= 1;
|
||||
g_string_truncate (address, newline - address->str);
|
||||
break;
|
||||
}
|
||||
|
||||
g_usleep (G_USEC_PER_SEC / 10);
|
||||
}
|
||||
|
||||
return g_string_free (address, FALSE);
|
||||
}
|
||||
|
||||
static DBusConnection *
|
||||
connect_to_bus (Fixture *f,
|
||||
const gchar *address)
|
||||
{
|
||||
DBusConnection *conn;
|
||||
DBusError error = DBUS_ERROR_INIT;
|
||||
dbus_bool_t ok;
|
||||
|
||||
conn = dbus_connection_open_private (address, &error);
|
||||
assert_no_error (&error);
|
||||
g_assert (conn != NULL);
|
||||
|
||||
ok = dbus_bus_register (conn, &error);
|
||||
assert_no_error (&error);
|
||||
g_assert (ok);
|
||||
g_assert (dbus_bus_get_unique_name (conn) != NULL);
|
||||
|
||||
test_connection_setup (f->ctx, conn);
|
||||
return conn;
|
||||
}
|
||||
|
||||
/* send a unicast signal to <self> to ensure that no other connection
|
||||
* listening is the actual recipient for the signal */
|
||||
static DBusHandlerResult
|
||||
|
|
@ -338,9 +233,9 @@ add_receiver_filter (Fixture *f)
|
|||
DBusError e = DBUS_ERROR_INIT;
|
||||
|
||||
dbus_bus_add_match (f->receiver, RECEIVER_RULE, &e);
|
||||
assert_no_error (&e);
|
||||
test_assert_no_error (&e);
|
||||
dbus_bus_add_match (f->receiver, STOPPER_RULE, &e);
|
||||
assert_no_error (&e);
|
||||
test_assert_no_error (&e);
|
||||
|
||||
if (!dbus_connection_add_filter (f->receiver,
|
||||
signal_filter, f, NULL))
|
||||
|
|
@ -353,9 +248,9 @@ add_eavesdropper_filter (Fixture *f)
|
|||
DBusError e = DBUS_ERROR_INIT;
|
||||
|
||||
dbus_bus_add_match (f->eavesdropper, EAVESDROPPER_RULE, &e);
|
||||
assert_no_error (&e);
|
||||
test_assert_no_error (&e);
|
||||
dbus_bus_add_match (f->eavesdropper, STOPPER_RULE, &e);
|
||||
assert_no_error (&e);
|
||||
test_assert_no_error (&e);
|
||||
|
||||
if (!dbus_connection_add_filter (f->eavesdropper,
|
||||
signal_filter, f, NULL))
|
||||
|
|
@ -368,9 +263,9 @@ add_politelistener_filter (Fixture *f)
|
|||
DBusError e = DBUS_ERROR_INIT;
|
||||
|
||||
dbus_bus_add_match (f->politelistener, POLITELISTENER_RULE, &e);
|
||||
assert_no_error (&e);
|
||||
test_assert_no_error (&e);
|
||||
dbus_bus_add_match (f->politelistener, STOPPER_RULE, &e);
|
||||
assert_no_error (&e);
|
||||
test_assert_no_error (&e);
|
||||
|
||||
if (!dbus_connection_add_filter (f->politelistener,
|
||||
signal_filter, f, NULL))
|
||||
|
|
@ -381,8 +276,6 @@ static void
|
|||
setup (Fixture *f,
|
||||
gconstpointer context G_GNUC_UNUSED)
|
||||
{
|
||||
gchar *dbus_daemon;
|
||||
gchar *config;
|
||||
gchar *address;
|
||||
|
||||
f->ctx = test_main_context_get ();
|
||||
|
|
@ -390,45 +283,14 @@ setup (Fixture *f,
|
|||
f->ge = NULL;
|
||||
dbus_error_init (&f->e);
|
||||
|
||||
dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
|
||||
address = test_get_dbus_daemon (NULL, &f->daemon_pid);
|
||||
|
||||
if (dbus_daemon == NULL)
|
||||
dbus_daemon = g_strdup ("dbus-daemon");
|
||||
|
||||
if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
|
||||
{
|
||||
config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
|
||||
g_getenv ("DBUS_TEST_SYSCONFDIR"));
|
||||
}
|
||||
else if (g_getenv ("DBUS_TEST_DATA") != NULL)
|
||||
{
|
||||
config = g_strdup_printf (
|
||||
"--config-file=%s/valid-config-files/session.conf",
|
||||
g_getenv ("DBUS_TEST_DATA"));
|
||||
}
|
||||
else
|
||||
{
|
||||
config = g_strdup ("--session");
|
||||
}
|
||||
|
||||
if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL)
|
||||
{
|
||||
address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS"));
|
||||
}
|
||||
else
|
||||
{
|
||||
address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
|
||||
}
|
||||
|
||||
g_free (dbus_daemon);
|
||||
g_free (config);
|
||||
|
||||
f->sender = connect_to_bus (f, address);
|
||||
f->sender = test_connect_to_bus (f->ctx, address);
|
||||
dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE,
|
||||
&(f->e));
|
||||
f->receiver = connect_to_bus (f, address);
|
||||
f->eavesdropper = connect_to_bus (f, address);
|
||||
f->politelistener = connect_to_bus (f, address);
|
||||
f->receiver = test_connect_to_bus (f->ctx, address);
|
||||
f->eavesdropper = test_connect_to_bus (f->ctx, address);
|
||||
f->politelistener = test_connect_to_bus (f->ctx, address);
|
||||
add_receiver_filter (f);
|
||||
add_politelistener_filter (f);
|
||||
add_eavesdropper_filter (f);
|
||||
|
|
@ -541,12 +403,7 @@ teardown (Fixture *f,
|
|||
f->eavesdropper = NULL;
|
||||
}
|
||||
|
||||
#ifdef DBUS_WIN
|
||||
TerminateProcess (f->daemon_pid, 1);
|
||||
#else
|
||||
kill (f->daemon_pid, SIGTERM);
|
||||
#endif
|
||||
|
||||
test_kill_pid (f->daemon_pid);
|
||||
g_spawn_close_pid (f->daemon_pid);
|
||||
|
||||
test_main_context_unref (f->ctx);
|
||||
|
|
|
|||
|
|
@ -26,23 +26,13 @@
|
|||
|
||||
#include <config.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include "test-utils-glib.h"
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifdef DBUS_WIN
|
||||
# include <io.h>
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <signal.h>
|
||||
#ifdef DBUS_UNIX
|
||||
# include <unistd.h>
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#include "test-utils.h"
|
||||
|
||||
/* Platforms where we know that credentials-passing passes both the
|
||||
* uid and the pid. Please keep these in alphabetical order.
|
||||
*
|
||||
|
|
@ -85,99 +75,6 @@ typedef struct {
|
|||
gboolean wait_forever_called;
|
||||
} Fixture;
|
||||
|
||||
#define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
|
||||
static void
|
||||
_assert_no_error (const DBusError *e,
|
||||
const char *file,
|
||||
int line)
|
||||
{
|
||||
if (G_UNLIKELY (dbus_error_is_set (e)))
|
||||
g_error ("%s:%d: expected success but got error: %s: %s",
|
||||
file, line, e->name, e->message);
|
||||
}
|
||||
|
||||
static gchar *
|
||||
spawn_dbus_daemon (gchar *binary,
|
||||
gchar *configuration,
|
||||
GPid *daemon_pid)
|
||||
{
|
||||
GError *error = NULL;
|
||||
GString *address;
|
||||
gint address_fd;
|
||||
gchar *argv[] = {
|
||||
binary,
|
||||
configuration,
|
||||
"--nofork",
|
||||
"--print-address=1", /* stdout */
|
||||
NULL
|
||||
};
|
||||
|
||||
g_spawn_async_with_pipes (NULL, /* working directory */
|
||||
argv,
|
||||
NULL, /* envp */
|
||||
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
|
||||
NULL, /* child_setup */
|
||||
NULL, /* user data */
|
||||
daemon_pid,
|
||||
NULL, /* child's stdin = /dev/null */
|
||||
&address_fd,
|
||||
NULL, /* child's stderr = our stderr */
|
||||
&error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
address = g_string_new (NULL);
|
||||
|
||||
/* polling until the dbus-daemon writes out its address is a bit stupid,
|
||||
* but at least it's simple, unlike dbus-launch... in principle we could
|
||||
* use select() here, but life's too short */
|
||||
while (1)
|
||||
{
|
||||
gssize bytes;
|
||||
gchar buf[4096];
|
||||
gchar *newline;
|
||||
|
||||
bytes = read (address_fd, buf, sizeof (buf));
|
||||
|
||||
if (bytes > 0)
|
||||
g_string_append_len (address, buf, bytes);
|
||||
|
||||
newline = strchr (address->str, '\n');
|
||||
|
||||
if (newline != NULL)
|
||||
{
|
||||
if ((newline > address->str) && ('\r' == newline[-1]))
|
||||
newline -= 1;
|
||||
g_string_truncate (address, newline - address->str);
|
||||
break;
|
||||
}
|
||||
|
||||
g_usleep (G_USEC_PER_SEC / 10);
|
||||
}
|
||||
|
||||
return g_string_free (address, FALSE);
|
||||
}
|
||||
|
||||
static DBusConnection *
|
||||
connect_to_bus (Fixture *f,
|
||||
const gchar *address)
|
||||
{
|
||||
DBusConnection *conn;
|
||||
DBusError error = DBUS_ERROR_INIT;
|
||||
dbus_bool_t ok;
|
||||
|
||||
conn = dbus_connection_open_private (address, &error);
|
||||
assert_no_error (&error);
|
||||
g_assert (conn != NULL);
|
||||
|
||||
ok = dbus_bus_register (conn, &error);
|
||||
assert_no_error (&error);
|
||||
g_assert (ok);
|
||||
g_assert (dbus_bus_get_unique_name (conn) != NULL);
|
||||
|
||||
test_connection_setup (f->ctx, conn);
|
||||
return conn;
|
||||
}
|
||||
|
||||
static DBusHandlerResult
|
||||
echo_filter (DBusConnection *connection,
|
||||
DBusMessage *message,
|
||||
|
|
@ -220,71 +117,23 @@ setup (Fixture *f,
|
|||
gconstpointer context)
|
||||
{
|
||||
const Config *config = context;
|
||||
gchar *dbus_daemon;
|
||||
gchar *arg;
|
||||
gchar *address;
|
||||
|
||||
f->ctx = test_main_context_get ();
|
||||
f->ge = NULL;
|
||||
dbus_error_init (&f->e);
|
||||
|
||||
if (config != NULL && config->config_file != NULL)
|
||||
{
|
||||
if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL)
|
||||
{
|
||||
g_message ("SKIP: cannot use DBUS_TEST_DAEMON_ADDRESS for "
|
||||
"unusally-configured dbus-daemon");
|
||||
f->skip = TRUE;
|
||||
return;
|
||||
}
|
||||
address = test_get_dbus_daemon (config ? config->config_file : NULL,
|
||||
&f->daemon_pid);
|
||||
|
||||
if (g_getenv ("DBUS_TEST_DATA") == NULL)
|
||||
{
|
||||
g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s",
|
||||
config->config_file);
|
||||
f->skip = TRUE;
|
||||
return;
|
||||
}
|
||||
|
||||
arg = g_strdup_printf (
|
||||
"--config-file=%s/%s",
|
||||
g_getenv ("DBUS_TEST_DATA"), config->config_file);
|
||||
}
|
||||
else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
|
||||
if (address == NULL)
|
||||
{
|
||||
arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
|
||||
g_getenv ("DBUS_TEST_SYSCONFDIR"));
|
||||
}
|
||||
else if (g_getenv ("DBUS_TEST_DATA") != NULL)
|
||||
{
|
||||
arg = g_strdup_printf (
|
||||
"--config-file=%s/valid-config-files/session.conf",
|
||||
g_getenv ("DBUS_TEST_DATA"));
|
||||
}
|
||||
else
|
||||
{
|
||||
arg = g_strdup ("--session");
|
||||
f->skip = TRUE;
|
||||
return;
|
||||
}
|
||||
|
||||
dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
|
||||
|
||||
if (dbus_daemon == NULL)
|
||||
dbus_daemon = g_strdup ("dbus-daemon");
|
||||
|
||||
if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL)
|
||||
{
|
||||
address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS"));
|
||||
}
|
||||
else
|
||||
{
|
||||
address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid);
|
||||
}
|
||||
|
||||
g_free (dbus_daemon);
|
||||
g_free (arg);
|
||||
|
||||
f->left_conn = connect_to_bus (f, address);
|
||||
f->right_conn = connect_to_bus (f, address);
|
||||
f->left_conn = test_connect_to_bus (f->ctx, address);
|
||||
f->right_conn = test_connect_to_bus (f->ctx, address);
|
||||
g_free (address);
|
||||
}
|
||||
|
||||
|
|
@ -366,16 +215,6 @@ test_echo (Fixture *f,
|
|||
count, elapsed);
|
||||
}
|
||||
|
||||
static void
|
||||
pending_call_store_reply (DBusPendingCall *pc,
|
||||
void *data)
|
||||
{
|
||||
DBusMessage **message_p = data;
|
||||
|
||||
*message_p = dbus_pending_call_steal_reply (pc);
|
||||
g_assert (*message_p != NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
test_no_reply (Fixture *f,
|
||||
gconstpointer context)
|
||||
|
|
@ -412,9 +251,9 @@ test_no_reply (Fixture *f,
|
|||
g_error ("OOM");
|
||||
|
||||
if (dbus_pending_call_get_completed (pc))
|
||||
pending_call_store_reply (pc, &reply);
|
||||
else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply, &reply,
|
||||
NULL))
|
||||
test_pending_call_store_reply (pc, &reply);
|
||||
else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
|
||||
&reply, NULL))
|
||||
g_error ("OOM");
|
||||
|
||||
dbus_pending_call_unref (pc);
|
||||
|
|
@ -485,8 +324,8 @@ test_creds (Fixture *f,
|
|||
m = NULL;
|
||||
|
||||
if (dbus_pending_call_get_completed (pc))
|
||||
pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply,
|
||||
test_pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
|
||||
&m, NULL))
|
||||
g_error ("OOM");
|
||||
|
||||
|
|
@ -596,8 +435,8 @@ test_processid (Fixture *f,
|
|||
m = NULL;
|
||||
|
||||
if (dbus_pending_call_get_completed (pc))
|
||||
pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply,
|
||||
test_pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
|
||||
&m, NULL))
|
||||
g_error ("OOM");
|
||||
|
||||
|
|
@ -609,7 +448,7 @@ test_processid (Fixture *f,
|
|||
DBUS_TYPE_INVALID))
|
||||
{
|
||||
g_assert_cmpstr (dbus_message_get_signature (m), ==, "u");
|
||||
assert_no_error (&error);
|
||||
test_assert_no_error (&error);
|
||||
|
||||
g_message ("GetConnectionUnixProcessID returned %u", pid);
|
||||
|
||||
|
|
@ -664,8 +503,8 @@ test_canonical_path_uae (Fixture *f,
|
|||
m = NULL;
|
||||
|
||||
if (dbus_pending_call_get_completed (pc))
|
||||
pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply,
|
||||
test_pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
|
||||
&m, NULL))
|
||||
g_error ("OOM");
|
||||
|
||||
|
|
@ -702,8 +541,8 @@ test_canonical_path_uae (Fixture *f,
|
|||
m = NULL;
|
||||
|
||||
if (dbus_pending_call_get_completed (pc))
|
||||
pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply,
|
||||
test_pending_call_store_reply (pc, &m);
|
||||
else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
|
||||
&m, NULL))
|
||||
g_error ("OOM");
|
||||
|
||||
|
|
@ -748,12 +587,7 @@ teardown (Fixture *f,
|
|||
|
||||
if (f->daemon_pid != 0)
|
||||
{
|
||||
#ifdef DBUS_WIN
|
||||
TerminateProcess (f->daemon_pid, 1);
|
||||
#else
|
||||
kill (f->daemon_pid, SIGTERM);
|
||||
#endif
|
||||
|
||||
test_kill_pid (f->daemon_pid);
|
||||
g_spawn_close_pid (f->daemon_pid);
|
||||
f->daemon_pid = 0;
|
||||
}
|
||||
|
|
|
|||
213
test/test-utils-glib.c
Normal file
213
test/test-utils-glib.c
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
/* Utility functions for tests that rely on GLib
|
||||
*
|
||||
* Copyright © 2010-2011 Nokia Corporation
|
||||
* Copyright © 2013-2015 Collabora Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include "test-utils-glib.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifdef DBUS_WIN
|
||||
# include <io.h>
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <signal.h>
|
||||
# include <unistd.h>
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
|
||||
void
|
||||
_test_assert_no_error (const DBusError *e,
|
||||
const char *file,
|
||||
int line)
|
||||
{
|
||||
if (G_UNLIKELY (dbus_error_is_set (e)))
|
||||
g_error ("%s:%d: expected success but got error: %s: %s",
|
||||
file, line, e->name, e->message);
|
||||
}
|
||||
|
||||
static gchar *
|
||||
spawn_dbus_daemon (const gchar *binary,
|
||||
const gchar *configuration,
|
||||
GPid *daemon_pid)
|
||||
{
|
||||
GError *error = NULL;
|
||||
GString *address;
|
||||
gint address_fd;
|
||||
const gchar *const argv[] = {
|
||||
binary,
|
||||
configuration,
|
||||
"--nofork",
|
||||
"--print-address=1", /* stdout */
|
||||
NULL
|
||||
};
|
||||
|
||||
g_spawn_async_with_pipes (NULL, /* working directory */
|
||||
(gchar **) argv, /* g_s_a_w_p() is not const-correct :-( */
|
||||
NULL, /* envp */
|
||||
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
|
||||
NULL, /* child_setup */
|
||||
NULL, /* user data */
|
||||
daemon_pid,
|
||||
NULL, /* child's stdin = /dev/null */
|
||||
&address_fd,
|
||||
NULL, /* child's stderr = our stderr */
|
||||
&error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
address = g_string_new (NULL);
|
||||
|
||||
/* polling until the dbus-daemon writes out its address is a bit stupid,
|
||||
* but at least it's simple, unlike dbus-launch... in principle we could
|
||||
* use select() here, but life's too short */
|
||||
while (1)
|
||||
{
|
||||
gssize bytes;
|
||||
gchar buf[4096];
|
||||
gchar *newline;
|
||||
|
||||
bytes = read (address_fd, buf, sizeof (buf));
|
||||
|
||||
if (bytes > 0)
|
||||
g_string_append_len (address, buf, bytes);
|
||||
|
||||
newline = strchr (address->str, '\n');
|
||||
|
||||
if (newline != NULL)
|
||||
{
|
||||
if ((newline > address->str) && ('\r' == newline[-1]))
|
||||
newline -= 1;
|
||||
g_string_truncate (address, newline - address->str);
|
||||
break;
|
||||
}
|
||||
|
||||
g_usleep (G_USEC_PER_SEC / 10);
|
||||
}
|
||||
|
||||
g_close (address_fd, NULL);
|
||||
|
||||
return g_string_free (address, FALSE);
|
||||
}
|
||||
|
||||
gchar *
|
||||
test_get_dbus_daemon (const gchar *config_file,
|
||||
GPid *daemon_pid)
|
||||
{
|
||||
gchar *dbus_daemon;
|
||||
gchar *arg;
|
||||
gchar *address;
|
||||
|
||||
if (config_file != NULL)
|
||||
{
|
||||
if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL)
|
||||
{
|
||||
g_message ("SKIP: cannot use DBUS_TEST_DAEMON_ADDRESS for "
|
||||
"unusally-configured dbus-daemon");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (g_getenv ("DBUS_TEST_DATA") == NULL)
|
||||
{
|
||||
g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s",
|
||||
config_file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
arg = g_strdup_printf (
|
||||
"--config-file=%s/%s",
|
||||
g_getenv ("DBUS_TEST_DATA"), config_file);
|
||||
}
|
||||
else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
|
||||
{
|
||||
arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
|
||||
g_getenv ("DBUS_TEST_SYSCONFDIR"));
|
||||
}
|
||||
else if (g_getenv ("DBUS_TEST_DATA") != NULL)
|
||||
{
|
||||
arg = g_strdup_printf (
|
||||
"--config-file=%s/valid-config-files/session.conf",
|
||||
g_getenv ("DBUS_TEST_DATA"));
|
||||
}
|
||||
else
|
||||
{
|
||||
arg = g_strdup ("--session");
|
||||
}
|
||||
|
||||
dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
|
||||
|
||||
if (dbus_daemon == NULL)
|
||||
dbus_daemon = g_strdup ("dbus-daemon");
|
||||
|
||||
if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL)
|
||||
{
|
||||
address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS"));
|
||||
}
|
||||
else
|
||||
{
|
||||
address = spawn_dbus_daemon (dbus_daemon, arg, daemon_pid);
|
||||
}
|
||||
|
||||
g_free (dbus_daemon);
|
||||
g_free (arg);
|
||||
return address;
|
||||
}
|
||||
|
||||
DBusConnection *
|
||||
test_connect_to_bus (TestMainContext *ctx,
|
||||
const gchar *address)
|
||||
{
|
||||
DBusConnection *conn;
|
||||
DBusError error = DBUS_ERROR_INIT;
|
||||
dbus_bool_t ok;
|
||||
|
||||
conn = dbus_connection_open_private (address, &error);
|
||||
test_assert_no_error (&error);
|
||||
g_assert (conn != NULL);
|
||||
|
||||
ok = dbus_bus_register (conn, &error);
|
||||
test_assert_no_error (&error);
|
||||
g_assert (ok);
|
||||
g_assert (dbus_bus_get_unique_name (conn) != NULL);
|
||||
|
||||
test_connection_setup (ctx, conn);
|
||||
return conn;
|
||||
}
|
||||
|
||||
void
|
||||
test_kill_pid (GPid pid)
|
||||
{
|
||||
#ifdef DBUS_WIN
|
||||
if (pid != NULL)
|
||||
TerminateProcess (pid, 1);
|
||||
#else
|
||||
if (pid > 0)
|
||||
kill (pid, SIGTERM);
|
||||
#endif
|
||||
}
|
||||
49
test/test-utils-glib.h
Normal file
49
test/test-utils-glib.h
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* Utility functions for tests that rely on GLib
|
||||
*
|
||||
* Copyright © 2010-2011 Nokia Corporation
|
||||
* Copyright © 2013-2015 Collabora Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TEST_UTILS_GLIB_H
|
||||
#define TEST_UTILS_GLIB_H
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "test-utils.h"
|
||||
|
||||
#define test_assert_no_error(e) _test_assert_no_error (e, __FILE__, __LINE__)
|
||||
void _test_assert_no_error (const DBusError *e,
|
||||
const char *file,
|
||||
int line);
|
||||
|
||||
gchar *test_get_dbus_daemon (const gchar *config_file,
|
||||
GPid *daemon_pid);
|
||||
|
||||
DBusConnection *test_connect_to_bus (TestMainContext *ctx,
|
||||
const gchar *address);
|
||||
|
||||
void test_kill_pid (GPid pid);
|
||||
|
||||
#endif
|
||||
|
|
@ -6,6 +6,14 @@
|
|||
# include <dbus/dbus-glib-lowlevel.h>
|
||||
#endif
|
||||
|
||||
#ifdef DBUS_TEST_USE_INTERNAL
|
||||
# define test_assert(x) _dbus_assert(x)
|
||||
#elif defined(g_assert_true)
|
||||
# define test_assert(x) g_assert_true(x)
|
||||
#else
|
||||
# define test_assert(x) g_assert(x)
|
||||
#endif
|
||||
|
||||
#ifdef DBUS_TEST_USE_INTERNAL
|
||||
|
||||
typedef struct
|
||||
|
|
@ -390,3 +398,13 @@ void test_main_context_iterate (TestMainContext *ctx,
|
|||
g_main_context_iteration (ctx, may_block);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
test_pending_call_store_reply (DBusPendingCall *pc,
|
||||
void *data)
|
||||
{
|
||||
DBusMessage **message_p = data;
|
||||
|
||||
*message_p = dbus_pending_call_steal_reply (pc);
|
||||
test_assert (*message_p != NULL);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,5 +34,7 @@ dbus_bool_t test_server_setup (TestMainContext *ctx,
|
|||
DBusServer *server);
|
||||
void test_server_shutdown (TestMainContext *ctx,
|
||||
DBusServer *server);
|
||||
void test_pending_call_store_reply (DBusPendingCall *pc,
|
||||
void *data);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue