PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/gio/tests/gdbus-error.c

https://bitbucket.org/weberw/glib
C | 269 lines | 157 code | 37 blank | 75 comment | 2 complexity | 0b59b294d898f19647b159560182736f MD5 | raw file
  1/* GLib testing framework examples and tests
  2 *
  3 * Copyright (C) 2008-2010 Red Hat, Inc.
  4 *
  5 * This library is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU Lesser General Public
  7 * License as published by the Free Software Foundation; either
  8 * version 2 of the License, or (at your option) any later version.
  9 *
 10 * This library is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13 * Lesser General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU Lesser General
 16 * Public License along with this library; if not, write to the
 17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 18 * Boston, MA 02111-1307, USA.
 19 *
 20 * Author: David Zeuthen <davidz@redhat.com>
 21 */
 22
 23#include <gio/gio.h>
 24#include <unistd.h>
 25#include <string.h>
 26
 27/* ---------------------------------------------------------------------------------------------------- */
 28/* Test that registered errors are properly mapped */
 29/* ---------------------------------------------------------------------------------------------------- */
 30
 31static void
 32check_registered_error (const gchar *given_dbus_error_name,
 33                        GQuark       error_domain,
 34                        gint         error_code)
 35{
 36  GError *error;
 37  gchar *dbus_error_name;
 38
 39  error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
 40  g_assert_error (error, error_domain, error_code);
 41  g_assert (g_dbus_error_is_remote_error (error));
 42  g_assert (g_dbus_error_strip_remote_error (error));
 43  g_assert_cmpstr (error->message, ==, "test message");
 44  dbus_error_name = g_dbus_error_get_remote_error (error);
 45  g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
 46  g_free (dbus_error_name);
 47  g_error_free (error);
 48}
 49
 50static void
 51test_registered_errors (void)
 52{
 53  /* Here we check that we are able to map to GError and back for registered
 54   * errors.
 55   *
 56   * For example, if "org.freedesktop.DBus.Error.AddressInUse" is
 57   * associated with (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED), check
 58   * that
 59   *
 60   *  - Creating a GError for e.g. "org.freedesktop.DBus.Error.AddressInUse"
 61   *    has (error_domain, code) == (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
 62   *
 63   *  - That it is possible to recover e.g. "org.freedesktop.DBus.Error.AddressInUse"
 64   *    as the D-Bus error name when dealing with an error with (error_domain, code) ==
 65   *    (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
 66   *
 67   * We just check a couple of well-known errors.
 68   */
 69  check_registered_error ("org.freedesktop.DBus.Error.Failed",
 70                          G_DBUS_ERROR,
 71                          G_DBUS_ERROR_FAILED);
 72  check_registered_error ("org.freedesktop.DBus.Error.AddressInUse",
 73                          G_DBUS_ERROR,
 74                          G_DBUS_ERROR_ADDRESS_IN_USE);
 75  check_registered_error ("org.freedesktop.DBus.Error.UnknownMethod",
 76                          G_DBUS_ERROR,
 77                          G_DBUS_ERROR_UNKNOWN_METHOD);
 78}
 79
 80/* ---------------------------------------------------------------------------------------------------- */
 81
 82static void
 83check_unregistered_error (const gchar *given_dbus_error_name)
 84{
 85  GError *error;
 86  gchar *dbus_error_name;
 87
 88  error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
 89  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
 90  g_assert (g_dbus_error_is_remote_error (error));
 91  dbus_error_name = g_dbus_error_get_remote_error (error);
 92  g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
 93  g_free (dbus_error_name);
 94
 95  /* strip the message */
 96  g_assert (g_dbus_error_strip_remote_error (error));
 97  g_assert_cmpstr (error->message, ==, "test message");
 98
 99  /* check that we can no longer recover the D-Bus error name */
100  g_assert (g_dbus_error_get_remote_error (error) == NULL);
101
102  g_error_free (error);
103
104}
105
106static void
107test_unregistered_errors (void)
108{
109  /* Here we check that we are able to map to GError and back for unregistered
110   * errors.
111   *
112   * For example, if "com.example.Error.Failed" is not registered, then check
113   *
114   *  - Creating a GError for e.g. "com.example.Error.Failed" has (error_domain, code) ==
115   *    (G_IO_ERROR, G_IO_ERROR_DBUS_ERROR)
116   *
117   *  - That it is possible to recover e.g. "com.example.Error.Failed" from that
118   *    GError.
119   *
120   * We just check a couple of random errors.
121   */
122
123  check_unregistered_error ("com.example.Error.Failed");
124  check_unregistered_error ("foobar.buh");
125}
126
127/* ---------------------------------------------------------------------------------------------------- */
128
129static void
130check_transparent_gerror (GQuark error_domain,
131                          gint   error_code)
132{
133  GError *error;
134  gchar *given_dbus_error_name;
135  gchar *dbus_error_name;
136
137  error = g_error_new (error_domain, error_code, "test message");
138  given_dbus_error_name = g_dbus_error_encode_gerror (error);
139  g_assert (g_str_has_prefix (given_dbus_error_name, "org.gtk.GDBus.UnmappedGError.Quark"));
140  g_error_free (error);
141
142  error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
143  g_assert_error (error, error_domain, error_code);
144  g_assert (g_dbus_error_is_remote_error (error));
145  dbus_error_name = g_dbus_error_get_remote_error (error);
146  g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
147  g_free (dbus_error_name);
148  g_free (given_dbus_error_name);
149
150  /* strip the message */
151  g_assert (g_dbus_error_strip_remote_error (error));
152  g_assert_cmpstr (error->message, ==, "test message");
153
154  /* check that we can no longer recover the D-Bus error name */
155  g_assert (g_dbus_error_get_remote_error (error) == NULL);
156
157  g_error_free (error);
158}
159
160static void
161test_transparent_gerror (void)
162{
163  /* Here we check that we are able to transparent pass unregistered GError's
164   * over the wire.
165   *
166   * For example, if G_IO_ERROR_FAILED is not registered, then check
167   *
168   *  - g_dbus_error_encode_gerror() returns something of the form
169   *    org.gtk.GDBus.UnmappedGError.Quark_HEXENCODED_QUARK_NAME_.Code_ERROR_CODE
170   *
171   *  - mapping back the D-Bus error name gives us G_IO_ERROR_FAILED
172   *
173   *  - That it is possible to recover the D-Bus error name from the
174   *    GError.
175   *
176   * We just check a couple of random errors.
177   */
178
179  check_transparent_gerror (G_IO_ERROR, G_IO_ERROR_FAILED);
180  check_transparent_gerror (G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
181}
182
183typedef enum
184{
185  TEST_ERROR_FAILED,
186  TEST_ERROR_BLA
187} TestError;
188
189GDBusErrorEntry test_error_entries[] =
190{
191  { TEST_ERROR_FAILED, "org.gtk.test.Error.Failed" },
192  { TEST_ERROR_BLA,    "org.gtk.test.Error.Bla"    }
193};
194
195static void
196test_register_error (void)
197{
198  gsize test_error_quark = 0;
199  gboolean res;
200  gchar *msg;
201  GError *error;
202
203  g_dbus_error_register_error_domain ("test-error-quark",
204                                      &test_error_quark,
205                                      test_error_entries,
206                                      G_N_ELEMENTS (test_error_entries));
207  g_assert_cmpint (test_error_quark, !=, 0);
208
209  error = g_dbus_error_new_for_dbus_error ("org.gtk.test.Error.Failed", "Failed");
210  g_assert_error (error, test_error_quark, TEST_ERROR_FAILED);
211  res = g_dbus_error_is_remote_error (error);
212  msg = g_dbus_error_get_remote_error (error);
213  g_assert (res);
214  g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
215  res = g_dbus_error_strip_remote_error (error);
216  g_assert (res);
217  g_assert_cmpstr (error->message, ==, "Failed");
218  g_clear_error (&error);
219  g_free (msg);
220
221  g_dbus_error_set_dbus_error (&error, "org.gtk.test.Error.Failed", "Failed again", "Prefix %d", 1);
222  res = g_dbus_error_is_remote_error (error);
223  msg = g_dbus_error_get_remote_error (error);
224  g_assert (res);
225  g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
226  res = g_dbus_error_strip_remote_error (error);
227  g_assert (res);
228  g_assert_cmpstr (error->message, ==, "Prefix 1: Failed again");
229  g_clear_error (&error);
230  g_free (msg);
231
232  error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_NOT_EMPTY, "Not Empty");
233  res = g_dbus_error_is_remote_error (error);
234  msg = g_dbus_error_get_remote_error (error);
235  g_assert (!res);
236  g_assert_cmpstr (msg, ==, NULL);
237  res = g_dbus_error_strip_remote_error (error);
238  g_assert (!res);
239  g_assert_cmpstr (error->message, ==, "Not Empty");
240  g_clear_error (&error);
241
242  error = g_error_new_literal (test_error_quark, TEST_ERROR_BLA, "Bla");
243  msg = g_dbus_error_encode_gerror (error);
244  g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Bla");
245  g_free (msg);
246  g_clear_error (&error);
247
248  res = g_dbus_error_unregister_error (test_error_quark,
249                                       TEST_ERROR_BLA, "org.gtk.test.Error.Bla");
250  g_assert (res);
251}
252
253
254/* ---------------------------------------------------------------------------------------------------- */
255
256int
257main (int   argc,
258      char *argv[])
259{
260  g_type_init ();
261  g_test_init (&argc, &argv, NULL);
262
263  g_test_add_func ("/gdbus/registered-errors", test_registered_errors);
264  g_test_add_func ("/gdbus/unregistered-errors", test_unregistered_errors);
265  g_test_add_func ("/gdbus/transparent-gerror", test_transparent_gerror);
266  g_test_add_func ("/gdbus/register-error", test_register_error);
267
268  return g_test_run();
269}