From 6af834d4a289c1adb8abfab619f86bd535d5050e Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 11 Feb 2016 14:10:41 +0000 Subject: [PATCH] Add a regression test for connection re-use Loosely based on loopback.c. This was an attempt to reproduce bug 94085. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=94085 Signed-off-by: Simon McVittie --- test/Makefile.am | 7 ++ test/shared-connection.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 test/shared-connection.c diff --git a/test/Makefile.am b/test/Makefile.am index b7f40bb..5bd2b89 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -151,6 +151,7 @@ installable_tests += \ test-marshal \ test-refs \ test-relay \ + test-shared-connection \ test-syntax \ test-syslog \ test-uid-permissions \ @@ -246,6 +247,12 @@ test_monitor_LDADD = \ $(GLIB_LIBS) \ $(NULL) +test_shared_connection_SOURCES = shared-connection.c +test_shared_connection_LDADD = \ + libdbus-testutils.la \ + $(GLIB_LIBS) \ + $(NULL) + test_syntax_SOURCES = syntax.c test_syntax_LDADD = \ libdbus-testutils.la \ diff --git a/test/shared-connection.c b/test/shared-connection.c new file mode 100644 index 0000000..108ac8c --- /dev/null +++ b/test/shared-connection.c @@ -0,0 +1,217 @@ +/* + * Copyright © 2010-2012 Nokia Corporation + * Copyright © 2015-2016 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 + +#include +#include + +#include + +#include +#include + +#include "test-utils-glib.h" + +typedef struct { + TestMainContext *ctx; + DBusError e; + + DBusServer *server; + DBusConnection *server_conns[10]; + DBusConnection *client_conns[10]; +} Fixture; + +static void +assert_no_error (const DBusError *e) +{ + if (G_UNLIKELY (dbus_error_is_set (e))) + g_error ("expected success but got error: %s: %s", e->name, e->message); +} + +static void +new_conn_cb (DBusServer *server, + DBusConnection *server_conn, + void *data) +{ + Fixture *f = data; + guint i; + + for (i = 0; i < G_N_ELEMENTS (f->server_conns); i++) + { + if (f->server_conns[i] == NULL) + { + f->server_conns[i] = dbus_connection_ref (server_conn); + test_connection_setup (f->ctx, server_conn); + return; + } + } + + /* we should never run out of slots because we have the same number of + * server and client connections */ + g_assert_not_reached (); +} + +static void +setup (Fixture *f, + gconstpointer addr) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (f->server_conns); i++) + f->server_conns[i] = NULL; + + for (i = 0; i < G_N_ELEMENTS (f->client_conns); i++) + f->client_conns[i] = NULL; + + f->ctx = test_main_context_get (); + dbus_error_init (&f->e); + + f->server = dbus_server_listen (addr, &f->e); + assert_no_error (&f->e); + g_assert (f->server != NULL); + + dbus_server_set_new_connection_function (f->server, + new_conn_cb, f, NULL); + test_server_setup (f->ctx, f->server); +} + +static void +test_no_reuse (Fixture *f, + gconstpointer addr G_GNUC_UNUSED) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (f->server_conns); i++) + g_assert (f->server_conns[i] == NULL); + + for (i = 0; i < G_N_ELEMENTS (f->client_conns); i++) + { + g_assert (f->client_conns[i] == NULL); + f->client_conns[i] = dbus_connection_open_private ( + dbus_server_get_address (f->server), &f->e); + assert_no_error (&f->e); + g_assert (f->client_conns[i] != NULL); + test_connection_setup (f->ctx, f->client_conns[i]); + } + + for (i = 0; i < G_N_ELEMENTS (f->server_conns); i++) + { + while (f->server_conns[i] == NULL) + { + test_progress ('.'); + test_main_context_iterate (f->ctx, TRUE); + } + } + + /* we can't do this in the shared teardown function because closing shared + * connections is not allowed */ + for (i = 0; i < G_N_ELEMENTS (f->client_conns); i++) + dbus_connection_close (f->client_conns[i]); +} + +static void +test_reuse (Fixture *f, + gconstpointer addr G_GNUC_UNUSED) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (f->server_conns); i++) + g_assert (f->server_conns[i] == NULL); + + for (i = 0; i < G_N_ELEMENTS (f->client_conns); i++) + { + g_assert (f->client_conns[i] == NULL); + f->client_conns[i] = dbus_connection_open ( + dbus_server_get_address (f->server), &f->e); + assert_no_error (&f->e); + g_assert (f->client_conns[i] != NULL); + test_connection_setup (f->ctx, f->client_conns[i]); + } + + for (i = 1; i < G_N_ELEMENTS (f->client_conns); i++) + g_assert (f->client_conns[i] == f->client_conns[0]); + + while (f->server_conns[0] == NULL) + { + test_progress ('.'); + test_main_context_iterate (f->ctx, TRUE); + } + + g_assert (f->server_conns[1] == NULL); +} + +static void +teardown (Fixture *f, + gconstpointer addr G_GNUC_UNUSED) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (f->client_conns); i++) + { + /* closing shared connections is not allowed */ + if (f->client_conns[i] != NULL) + dbus_connection_unref (f->client_conns[i]); + } + + for (i = 0; i < G_N_ELEMENTS (f->server_conns); i++) + { + if (f->server_conns[i] != NULL) + { + dbus_connection_close (f->server_conns[i]); + dbus_connection_unref (f->server_conns[i]); + } + } + + if (f->server != NULL) + { + dbus_server_disconnect (f->server); + dbus_server_unref (f->server); + f->server = NULL; + } + + test_main_context_unref (f->ctx); +} + +int +main (int argc, + char **argv) +{ + test_init (&argc, &argv); + + g_test_add ("/reuse/tcp", Fixture, "tcp:host=127.0.0.1", setup, + test_reuse, teardown); + g_test_add ("/no-reuse/tcp", Fixture, "tcp:host=127.0.0.1", setup, + test_no_reuse, teardown); + +#ifdef DBUS_UNIX + g_test_add ("/reuse/unix", Fixture, "unix:tmpdir=/tmp", setup, + test_reuse, teardown); + g_test_add ("/no-reuse/unix", Fixture, "unix:tmpdir=/tmp", setup, + test_no_reuse, teardown); +#endif + + return g_test_run (); +} -- 2.7.0