1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* Feature test for exported object methods raising errors
*
* Copyright © 2006-2010 Red Hat, Inc.
* Copyright © 2006-2010 Collabora Ltd.
* Copyright © 2006-2011 Nokia Corporation
* Copyright © 2006 Steve Frécinaux
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <config.h>
#undef G_DISABLE_ASSERT
#include <glib.h>
#include <gio/gio.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <string.h>
#include "my-object.h"
#include "test-service-glib-bindings.h"
/* my-object wants this to exist */
GMainLoop *loop = NULL;
typedef struct {
GError *error;
gchar *error_name;
DBusGConnection *conn;
DBusGProxy *proxy;
GObject *object;
} Fixture;
#define assert_contains(haystack, needle) \
assert_contains_impl (__FILE__, __LINE__, G_STRINGIFY (haystack), haystack, \
G_STRINGIFY (needle), needle)
static void
assert_contains_impl (const gchar *file,
gint line,
const gchar *haystack_desc,
const gchar *haystack,
const gchar *needle_desc,
const gchar *needle)
{
if (G_UNLIKELY (strstr (haystack, needle) == NULL))
{
g_error ("%s:%d: assertion failed: (%s) contains (%s): "
"values are \"%s\", \"%s\"",
file, line, haystack_desc, needle_desc, haystack, needle);
}
}
static void
setup (Fixture *f,
gconstpointer context)
{
static gsize once = 0;
dbus_g_type_specialized_init ();
if (g_once_init_enter (&once))
{
/* this may only be called once */
dbus_g_error_domain_register (MY_OBJECT_ERROR, NULL, MY_TYPE_ERROR);
g_once_init_leave (&once, 1);
}
f->conn = dbus_g_bus_get_private (DBUS_BUS_SESSION, NULL, &f->error);
g_assert_no_error (f->error);
g_assert_nonnull (f->conn);
f->object = g_object_new (MY_TYPE_OBJECT, NULL);
g_assert_true (MY_IS_OBJECT (f->object));
dbus_g_connection_register_g_object (f->conn, "/com/example/Test/Object",
f->object);
f->proxy = dbus_g_proxy_new_for_name (f->conn,
dbus_bus_get_unique_name (dbus_g_connection_get_connection (f->conn)),
"/com/example/Test/Object", "org.freedesktop.DBus.GLib.Tests.MyObject");
g_assert_nonnull (f->proxy);
}
static void
throw_error_cb (DBusGProxy *proxy,
GError *error,
gpointer user_data)
{
Fixture *f = user_data;
g_assert_nonnull (error);
g_clear_error (&f->error);
g_free (f->error_name);
f->error = g_error_copy (error);
if (g_error_matches (error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION))
f->error_name = g_strdup (dbus_g_error_get_name (error));
else
f->error_name = NULL;
/* On error, this callback is expected to free it, which is pretty
* astonishing, but is how it's always been. In principle, the generated
* code ought to document this, or something.
* https://bugs.freedesktop.org/show_bug.cgi?id=29195
*/
g_error_free (error);
}
static void
test_async (Fixture *f,
gconstpointer context)
{
/* This is equivalent to test_simple but uses a method that's implemented
* async at the service side - it's a different calling convention for the
* service, but is indistinguishable here. */
my_object_save_error ((MyObject *) f->object, MY_OBJECT_ERROR,
MY_OBJECT_ERROR_FOO, "<foo>");
if (!org_freedesktop_DBus_GLib_Tests_MyObject_async_throw_error_async (
f->proxy, throw_error_cb, f))
g_error ("Failed to start async AsyncThrowError call");
while (f->error == NULL)
g_main_context_iteration (NULL, TRUE);
g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
g_assert_cmpstr (f->error_name, ==,
"org.freedesktop.DBus.GLib.Tests.MyObject.Foo");
assert_contains (f->error->message, "<foo>");
}
static void
test_simple (Fixture *f,
gconstpointer context)
{
my_object_save_error ((MyObject *) f->object, MY_OBJECT_ERROR,
MY_OBJECT_ERROR_FOO, "<foo>");
if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
f->proxy, throw_error_cb, f))
g_error ("Failed to start async ThrowError call");
while (f->error == NULL)
g_main_context_iteration (NULL, TRUE);
g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
g_assert_cmpstr (f->error_name, ==,
"org.freedesktop.DBus.GLib.Tests.MyObject.Foo");
assert_contains (f->error->message, "<foo>");
}
static void
test_builtin (Fixture *f,
gconstpointer context)
{
g_test_bug ("16776");
my_object_save_error ((MyObject *) f->object, DBUS_GERROR,
DBUS_GERROR_NOT_SUPPORTED, "<not supported>");
if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
f->proxy, throw_error_cb, f))
g_error ("Failed to start async ThrowError call");
while (f->error == NULL)
g_main_context_iteration (NULL, TRUE);
g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_NOT_SUPPORTED);
assert_contains (f->error->message, "<not supported>");
}
static void
test_multi_word (Fixture *f,
gconstpointer context)
{
/* no bug#, but this is a regression test for commit 3d69cfeab177e */
my_object_save_error ((MyObject *) f->object, MY_OBJECT_ERROR,
MY_OBJECT_ERROR_MULTI_WORD, "this method's error has a hyphen");
if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
f->proxy, throw_error_cb, f))
g_error ("Failed to start async ThrowError call");
while (f->error == NULL)
g_main_context_iteration (NULL, TRUE);
g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
g_assert_cmpstr (f->error_name, ==,
"org.freedesktop.DBus.GLib.Tests.MyObject.MultiWord");
assert_contains (f->error->message, "this method's error has a hyphen");
}
static void
test_underscore (Fixture *f,
gconstpointer context)
{
g_test_bug ("30274");
my_object_save_error ((MyObject *) f->object, MY_OBJECT_ERROR,
MY_OBJECT_ERROR_UNDER_SCORE, "this method's error has an underscore");
if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
f->proxy, throw_error_cb, f))
g_error ("Failed to start async ThrowError call");
while (f->error == NULL)
g_main_context_iteration (NULL, TRUE);
g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
g_assert_cmpstr (f->error_name, ==,
"org.freedesktop.DBus.GLib.Tests.MyObject.Under_score");
assert_contains (f->error->message, "this method's error has an underscore");
}
static void
test_unregistered (Fixture *f,
gconstpointer context)
{
g_test_bug ("27799");
my_object_save_error ((MyObject *) f->object, G_IO_ERROR,
G_IO_ERROR_NOT_INITIALIZED,
"dbus-glib does not know about this error domain");
if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
f->proxy, throw_error_cb, f))
g_error ("Failed to start async ThrowError call");
while (f->error == NULL)
g_main_context_iteration (NULL, TRUE);
g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
assert_contains (f->error->message,
"dbus-glib does not know about this error domain");
}
static void
teardown (Fixture *f,
gconstpointer context G_GNUC_UNUSED)
{
g_free (f->error_name);
g_clear_error (&f->error);
if (f->proxy != NULL)
{
g_object_unref (f->proxy);
f->proxy = NULL;
}
if (f->object != NULL)
{
g_object_unref (f->object);
f->object = NULL;
}
if (f->conn != NULL)
{
dbus_connection_close (dbus_g_connection_get_connection (f->conn));
dbus_g_connection_unref (f->conn);
f->conn = NULL;
}
}
int
main (int argc,
char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
g_type_init ();
g_test_add ("/error-mapping/async", Fixture, NULL, setup, test_async,
teardown);
g_test_add ("/error-mapping/builtin", Fixture, NULL, setup, test_builtin,
teardown);
g_test_add ("/error-mapping/multi-word", Fixture, NULL, setup,
test_multi_word, teardown);
g_test_add ("/error-mapping/simple", Fixture, NULL, setup, test_simple,
teardown);
g_test_add ("/error-mapping/underscore", Fixture, NULL, setup,
test_underscore, teardown);
g_test_add ("/error-mapping/unregistered", Fixture, NULL, setup,
test_unregistered, teardown);
return g_test_run ();
}
|