glib/gio/tests/gdbus-error.c
David Zeuthen d0a14469d0 Initial GDBus code-drop from GDBus-standalone repo
Things compile and the test-suite passes. Still need to hook up
gio.symbols and docs. There are still a bunch of TODOs left in the
sources that needs to be addressed.

Signed-off-by: David Zeuthen <davidz@redhat.com>
2010-05-06 14:21:32 -04:00

199 lines
6.8 KiB
C

/* GLib testing framework examples and tests
*
* Copyright (C) 2008-2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <gio/gio.h>
#include <unistd.h>
#include <string.h>
/* ---------------------------------------------------------------------------------------------------- */
/* Test that registered errors are properly mapped */
/* ---------------------------------------------------------------------------------------------------- */
static void
check_registered_error (const gchar *given_dbus_error_name,
GQuark error_domain,
gint error_code)
{
GError *error;
gchar *dbus_error_name;
error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
g_assert_error (error, error_domain, error_code);
g_assert (g_dbus_error_is_remote_error (error));
g_assert (g_dbus_error_strip_remote_error (error));
g_assert_cmpstr (error->message, ==, "test message");
dbus_error_name = g_dbus_error_get_remote_error (error);
g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
g_free (dbus_error_name);
g_error_free (error);
}
static void
test_registered_errors (void)
{
/* Here we check that we are able to map to GError and back for registered
* errors.
*
* For example, if "org.freedesktop.DBus.Error.AddressInUse" is
* associated with (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED), check
* that
*
* - Creating a GError for e.g. "org.freedesktop.DBus.Error.AddressInUse"
* has (error_domain, code) == (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
*
* - That it is possible to recover e.g. "org.freedesktop.DBus.Error.AddressInUse"
* as the D-Bus error name when dealing with an error with (error_domain, code) ==
* (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
*
* We just check a couple of well-known errors.
*/
check_registered_error ("org.freedesktop.DBus.Error.Failed",
G_DBUS_ERROR,
G_DBUS_ERROR_FAILED);
check_registered_error ("org.freedesktop.DBus.Error.AddressInUse",
G_DBUS_ERROR,
G_DBUS_ERROR_ADDRESS_IN_USE);
check_registered_error ("org.freedesktop.DBus.Error.UnknownMethod",
G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_METHOD);
}
/* ---------------------------------------------------------------------------------------------------- */
static void
check_unregistered_error (const gchar *given_dbus_error_name)
{
GError *error;
gchar *dbus_error_name;
error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
g_assert (g_dbus_error_is_remote_error (error));
dbus_error_name = g_dbus_error_get_remote_error (error);
g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
g_free (dbus_error_name);
/* strip the message */
g_assert (g_dbus_error_strip_remote_error (error));
g_assert_cmpstr (error->message, ==, "test message");
/* check that we can no longer recover the D-Bus error name */
g_assert (g_dbus_error_get_remote_error (error) == NULL);
g_error_free (error);
}
static void
test_unregistered_errors (void)
{
/* Here we check that we are able to map to GError and back for unregistered
* errors.
*
* For example, if "com.example.Error.Failed" is not registered, then check
*
* - Creating a GError for e.g. "com.example.Error.Failed" has (error_domain, code) ==
* (G_IO_ERROR, G_IO_ERROR_DBUS_ERROR)
*
* - That it is possible to recover e.g. "com.example.Error.Failed" from that
* GError.
*
* We just check a couple of random errors.
*/
check_unregistered_error ("com.example.Error.Failed");
check_unregistered_error ("foobar.buh");
}
/* ---------------------------------------------------------------------------------------------------- */
static void
check_transparent_gerror (GQuark error_domain,
gint error_code)
{
GError *error;
gchar *given_dbus_error_name;
gchar *dbus_error_name;
error = g_error_new (error_domain, error_code, "test message");
given_dbus_error_name = g_dbus_error_encode_gerror (error);
g_assert (g_str_has_prefix (given_dbus_error_name, "org.gtk.GDBus.UnmappedGError.Quark"));
g_error_free (error);
error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
g_assert_error (error, error_domain, error_code);
g_assert (g_dbus_error_is_remote_error (error));
dbus_error_name = g_dbus_error_get_remote_error (error);
g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
g_free (dbus_error_name);
g_free (given_dbus_error_name);
/* strip the message */
g_assert (g_dbus_error_strip_remote_error (error));
g_assert_cmpstr (error->message, ==, "test message");
/* check that we can no longer recover the D-Bus error name */
g_assert (g_dbus_error_get_remote_error (error) == NULL);
g_error_free (error);
}
static void
test_transparent_gerror (void)
{
/* Here we check that we are able to transparent pass unregistered GError's
* over the wire.
*
* For example, if G_IO_ERROR_FAILED is not registered, then check
*
* - g_dbus_error_encode_gerror() returns something of the form
* org.gtk.GDBus.UnmappedGError.Quark_HEXENCODED_QUARK_NAME_.Code_ERROR_CODE
*
* - mapping back the D-Bus error name gives us G_IO_ERROR_FAILED
*
* - That it is possible to recover the D-Bus error name from the
* GError.
*
* We just check a couple of random errors.
*/
check_transparent_gerror (G_IO_ERROR, G_IO_ERROR_FAILED);
check_transparent_gerror (G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
}
/* ---------------------------------------------------------------------------------------------------- */
int
main (int argc,
char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gdbus/registered-errors", test_registered_errors);
g_test_add_func ("/gdbus/unregistered-errors", test_unregistered_errors);
g_test_add_func ("/gdbus/transparent-gerror", test_transparent_gerror);
return g_test_run();
}