From dd352d78a7ca498e43fc847fd69578846dfebc3f Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 3 May 2011 15:25:08 +0100 Subject: [PATCH 3/6] Subsume the test for #5688 into the more general registrations test Also test that the object is unregistered by the last unref or by forced disposal, without crashing. --- test/core/5688.c | 81 --------------------------------------------- test/core/Makefile.am | 9 ----- test/core/registrations.c | 73 +++++++++++++++++++++++++++++++++++++--- test/core/run-test.sh | 1 - 4 files changed, 67 insertions(+), 97 deletions(-) delete mode 100644 test/core/5688.c diff --git a/test/core/5688.c b/test/core/5688.c deleted file mode 100644 index c08240d..0000000 --- a/test/core/5688.c +++ /dev/null @@ -1,81 +0,0 @@ -/* Regression test for freedesktop.org #5688. - * - * Copyright © 2009 Collabora Ltd. - * Copyright © 2009 Nokia Corporation - * - * In preparation for dbus-glib relicensing (if it ever happens), this file is - * licensed under (at your option) either the AFL v2.1, the GPL v2 or later, - * or an MIT/X11-style license: - * - * Licensed under the Academic Free License version 2.1 - * - * 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. - * - * 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 "dbus/dbus-gutils.h" /* for DBUS_G_CONNECTION_FROM_CONNECTION */ - -#include "my-object.h" - -GMainLoop *loop; - -int -main (int argc, char **argv) -{ - DBusConnection *d_connection; - DBusGConnection *connection; - DBusError d_error; - GObject *obj; - - dbus_error_init (&d_error); - loop = g_main_loop_new (NULL, FALSE); - - g_type_init (); - g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL); - - d_connection = dbus_bus_get_private (DBUS_BUS_SESSION, NULL); - - if (d_connection == NULL) - g_error ("%s: %s", d_error.name, d_error.message); - - dbus_connection_setup_with_g_main (d_connection, NULL); - - connection = DBUS_G_CONNECTION_FROM_CONNECTION (d_connection); - - obj = g_object_new (MY_TYPE_OBJECT, NULL); - dbus_g_connection_register_g_object (connection, "/foo", obj); - g_assert (dbus_g_connection_lookup_g_object (connection, "/foo") == obj); - g_assert (dbus_g_connection_lookup_g_object (connection, "/bar") == NULL); - - dbus_connection_close (d_connection); - dbus_connection_unref (d_connection); - - g_object_unref (obj); - - return 0; -} diff --git a/test/core/Makefile.am b/test/core/Makefile.am index 82fb8f7..cdd53ce 100644 --- a/test/core/Makefile.am +++ b/test/core/Makefile.am @@ -53,20 +53,11 @@ noinst_PROGRAMS = \ peer-server \ peer-client \ test-types \ - test-5688 \ test-30574 \ test-registrations \ test-variant-recursion \ test-gvariant -test_5688_SOURCES = \ - my-object.c \ - my-object.h \ - my-object-subclass.c \ - my-object-subclass.h \ - my-object-marshal.c \ - 5688.c - test_30574_SOURCES = \ 30574.c diff --git a/test/core/registrations.c b/test/core/registrations.c index c3fe545..befef06 100644 --- a/test/core/registrations.c +++ b/test/core/registrations.c @@ -1,4 +1,4 @@ -/* Feature test for freedesktop.org #21219 and similar. +/* Regression test for object registration and unregistration * * Copyright © 2009 Collabora Ltd. * Copyright © 2009-2011 Nokia Corporation @@ -62,22 +62,42 @@ static void teardown (Fixture *f, gconstpointer test_data G_GNUC_UNUSED) { - if (f->object != NULL) - { - g_object_unref (f->object); - } - + /* we close the connection before releasing the object, to test fd.o #5688 + * in test_lookup() */ if (f->bus != NULL) { dbus_connection_close (dbus_g_connection_get_connection (f->bus)); dbus_g_connection_unref (f->bus); } + + if (f->object != NULL) + { + g_object_unref (f->object); + } +} + +static void +test_lookup (Fixture *f, + gconstpointer test_data G_GNUC_UNUSED) +{ + /* teardown() closes the connection before f->object is destroyed, which + * used to be broken */ + g_test_bug ("5688"); + + dbus_g_connection_register_g_object (f->bus, "/foo", f->object); + g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == + f->object); + /* this was briefly broken while fixing fd.o#5688 */ + g_assert (dbus_g_connection_lookup_g_object (f->bus, "/bar") == NULL); } static void test_unregister (Fixture *f, gconstpointer test_data G_GNUC_UNUSED) { + /* feature test: objects can be unregistered */ + g_test_bug ("21219"); + dbus_g_connection_register_g_object (f->bus, "/foo", f->object); g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == f->object); @@ -85,6 +105,41 @@ test_unregister (Fixture *f, g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == NULL); } +static void +test_unregister_on_last_unref (Fixture *f, + gconstpointer test_data G_GNUC_UNUSED) +{ + gpointer weak_pointer; + + weak_pointer = f->object; + g_object_add_weak_pointer (weak_pointer, &weak_pointer); + + dbus_g_connection_register_g_object (f->bus, "/foo", f->object); + g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == + f->object); + /* implicit unregistration by the last-unref of the object */ + g_object_unref (f->object); + f->object = NULL; + + g_assert (weak_pointer == NULL); + + g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == NULL); +} + +static void +test_unregister_on_forced_dispose (Fixture *f, + gconstpointer test_data G_GNUC_UNUSED) +{ + dbus_g_connection_register_g_object (f->bus, "/foo", f->object); + g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == + f->object); + /* implicit unregistration by dispose() of the object (don't try + * this at home) */ + g_object_run_dispose (f->object); + + g_assert (dbus_g_connection_lookup_g_object (f->bus, "/foo") == NULL); +} + int main (int argc, char **argv) { @@ -96,8 +151,14 @@ main (int argc, char **argv) g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id="); g_test_init (&argc, &argv, NULL); + g_test_add ("/registrations/lookup", Fixture, NULL, + setup, test_lookup, teardown); g_test_add ("/registrations/unregister", Fixture, NULL, setup, test_unregister, teardown); + g_test_add ("/registrations/unregister-on-last-unref", Fixture, NULL, + setup, test_unregister_on_last_unref, teardown); + g_test_add ("/registrations/unregister-on-forced-dispose", Fixture, NULL, + setup, test_unregister_on_forced_dispose, teardown); return g_test_run (); } diff --git a/test/core/run-test.sh b/test/core/run-test.sh index 3088ac4..09e28b1 100755 --- a/test/core/run-test.sh +++ b/test/core/run-test.sh @@ -44,7 +44,6 @@ else dbus-monitor --session & fi ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/core/test-types || die "test-types failed" - ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/core/test-5688 || die "test-5688 failed" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/core/test-registrations || die "test-registrations failed" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/core/test-dbus-glib || die "test-dbus-glib failed" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/core/test-variant-recursion || die "test-variant-recursion failed" -- 1.7.4.4