summaryrefslogtreecommitdiff
path: root/gio/tests/gdbus-error.c
blob: 96ed42257acb7f91e230023f3a561ab763eaa59a (plain)
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
/* GLib testing framework examples and tests
 *
 * Copyright (C) 2008-2010 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.1 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, see <http://www.gnu.org/licenses/>.
 *
 * 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);
  check_registered_error ("org.freedesktop.DBus.Error.UnknownObject",
                          G_DBUS_ERROR,
                          G_DBUS_ERROR_UNKNOWN_OBJECT);
}

/* ---------------------------------------------------------------------------------------------------- */

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);
}

typedef enum
{
  TEST_ERROR_FAILED,
  TEST_ERROR_BLA
} TestError;

GDBusErrorEntry test_error_entries[] =
{
  { TEST_ERROR_FAILED, "org.gtk.test.Error.Failed" },
  { TEST_ERROR_BLA,    "org.gtk.test.Error.Bla"    }
};

static void
test_register_error (void)
{
  gsize test_error_quark = 0;
  gboolean res;
  gchar *msg;
  GError *error;

  g_dbus_error_register_error_domain ("test-error-quark",
                                      &test_error_quark,
                                      test_error_entries,
                                      G_N_ELEMENTS (test_error_entries));
  g_assert_cmpint (test_error_quark, !=, 0);

  error = g_dbus_error_new_for_dbus_error ("org.gtk.test.Error.Failed", "Failed");
  g_assert_error (error, test_error_quark, TEST_ERROR_FAILED);
  res = g_dbus_error_is_remote_error (error);
  msg = g_dbus_error_get_remote_error (error);
  g_assert (res);
  g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
  res = g_dbus_error_strip_remote_error (error);
  g_assert (res);
  g_assert_cmpstr (error->message, ==, "Failed");
  g_clear_error (&error);
  g_free (msg);

  g_dbus_error_set_dbus_error (&error, "org.gtk.test.Error.Failed", "Failed again", "Prefix %d", 1);
  res = g_dbus_error_is_remote_error (error);
  msg = g_dbus_error_get_remote_error (error);
  g_assert (res);
  g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
  res = g_dbus_error_strip_remote_error (error);
  g_assert (res);
  g_assert_cmpstr (error->message, ==, "Prefix 1: Failed again");
  g_clear_error (&error);
  g_free (msg);

  error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_NOT_EMPTY, "Not Empty");
  res = g_dbus_error_is_remote_error (error);
  msg = g_dbus_error_get_remote_error (error);
  g_assert (!res);
  g_assert_cmpstr (msg, ==, NULL);
  res = g_dbus_error_strip_remote_error (error);
  g_assert (!res);
  g_assert_cmpstr (error->message, ==, "Not Empty");
  g_clear_error (&error);

  error = g_error_new_literal (test_error_quark, TEST_ERROR_BLA, "Bla");
  msg = g_dbus_error_encode_gerror (error);
  g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Bla");
  g_free (msg);
  g_clear_error (&error);

  res = g_dbus_error_unregister_error (test_error_quark,
                                       TEST_ERROR_BLA, "org.gtk.test.Error.Bla");
  g_assert (res);
}


/* ---------------------------------------------------------------------------------------------------- */

int
main (int   argc,
      char *argv[])
{
  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);
  g_test_add_func ("/gdbus/register-error", test_register_error);

  return g_test_run();
}