summaryrefslogtreecommitdiff
path: root/test/core/manual/invalid-usage.c
blob: 383c1f5f0ffe01ee411493676280757f337db715 (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
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
312
313
314
315
316
317
318
319
320
321
322
323
/* Manual test for various invalid usages which should not crash us (in order
 * to be nice to fallible programmers), unless checks have been disabled (in
 * which case, you asked for it, you got it).
 *
 * 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 <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;
    DBusGConnection *conn;
    DBusGProxy *proxy;
    DBusGProxy *proxy_for_self;
    GObject *object;
} Fixture;

static void
setup (Fixture *f,
    gconstpointer context)
{
  /* this test is all about (mostly critical) warnings, so don't crash out on
   * programming errors */
  g_setenv ("DBUS_FATAL_WARNINGS", "0", TRUE);
  g_log_set_always_fatal (G_LOG_LEVEL_ERROR);

  dbus_g_type_specialized_init ();

  /* This is a bug: you're not meant to register any domain more than
   * once. It shouldn't crash, though. */
  dbus_g_error_domain_register (MY_OBJECT_ERROR, NULL, MY_TYPE_ERROR);

  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->proxy = dbus_g_proxy_new_for_name (f->conn, "com.example.Test",
      "/com/example/Test/Object", "com.example.Test.Fallible");
  g_assert_nonnull (f->proxy);

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

static void
test_invalid_gtype (Fixture *f,
    gconstpointer context)
{
  /* G_TYPE_GTYPE is not handled by the dbus-glib type system (and would make
   * no sense anyway) */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      G_TYPE_GTYPE, G_TYPE_STRING,
      G_TYPE_INVALID);
}

static void
test_invalid_utf8 (Fixture *f,
    gconstpointer context)
{
  g_test_bug ("30171");

  /* This provokes a libdbus warning, which is fatal-by-default */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      G_TYPE_STRING, "\xfe\xfe\xfe",
      G_TYPE_INVALID);
}

static void
test_invalid_bool (Fixture *f,
    gconstpointer context)
{
  g_test_bug ("30171");

  /* This provokes a libdbus warning, which is fatal-by-default */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      G_TYPE_BOOLEAN, (gboolean) (-42),
      G_TYPE_INVALID);
}

static void
test_invalid_path (Fixture *f,
    gconstpointer context)
{
  g_test_bug ("30171");

  /* This provokes a libdbus warning, which is fatal-by-default */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      DBUS_TYPE_G_OBJECT_PATH, "$%#*!",
      G_TYPE_INVALID);
}

static void
test_invalid_utf8s (Fixture *f,
    gconstpointer context)
{
  gchar *bad_strings[] = { "\xfe\xfe\xfe", NULL };
  GStrv bad_strv = bad_strings;

  g_test_bug ("30171");

  /* This provokes a libdbus warning, which is fatal-by-default */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      G_TYPE_STRV, bad_strv,
      G_TYPE_INVALID);
}

static void
test_invalid_bools (Fixture *f,
    gconstpointer context)
{
  GArray *array;
  gboolean maybe = (gboolean) (-23);

  g_test_bug ("30171");

  array = g_array_new (FALSE, FALSE, sizeof (gboolean));

  g_array_append_val (array, maybe);

  /* This provokes a libdbus warning, which is fatal-by-default */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      dbus_g_type_get_collection ("GArray", G_TYPE_BOOLEAN), array,
      G_TYPE_INVALID);

  g_array_free (array, TRUE);
}

static void
test_invalid_paths (Fixture *f,
    gconstpointer context)
{
  GPtrArray *array;

  g_test_bug ("30171");

  array = g_ptr_array_new ();
  g_ptr_array_add (array, "bees");

  /* This provokes a libdbus warning, which is fatal-by-default */
  dbus_g_proxy_call_no_reply (f->proxy, "Fail",
      dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), array,
      G_TYPE_INVALID);

  g_ptr_array_free (array, TRUE);
}

static void
throw_error_cb (DBusGProxy *proxy,
    GError *error,
    gpointer user_data)
{
  GError **error_out = user_data;

  g_assert_nonnull (error);
  *error_out = g_error_copy (error);
}

static void
test_error_out_of_range (Fixture *f,
    gconstpointer context)
{
  GError *error = NULL;

  g_test_bug ("40151");

  /* This is a bug: -1 isn't a valid code for the domain. */
  my_object_save_error ((MyObject *) f->object, MY_OBJECT_ERROR, -1,
      "stop being so negative");

  if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
        f->proxy_for_self, throw_error_cb, &error))
    g_error ("Failed to start async ThrowError call");

  while (error == NULL)
    g_main_context_iteration (NULL, TRUE);

  g_assert_error (error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
  g_clear_error (&error);

  /* This is a bug: 666 isn't a valid code for the domain. */
  my_object_save_error ((MyObject *) f->object, MY_OBJECT_ERROR, 666,
      "demonic possession detected");

  if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_error_async (
        f->proxy_for_self, throw_error_cb, &error))
    g_error ("Failed to start async ThrowError call");

  while (error == NULL)
    g_main_context_iteration (NULL, TRUE);

  g_assert_error (error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
  g_clear_error (&error);
}

static void
test_error_domain_0 (Fixture *f,
    gconstpointer context)
{
  /* This throws an error with domain 0 and code 0, which makes no sense.
   * It's programmer error, really: g_error_new() would critical if given
   * the same domain and code. See GNOME#660371.
   *
   * This was added for fd.o #27799, but there's a difference between
   * "this is an error domain, but not one registered with dbus-glib" and
   * "this isn't even an error domain". */
  g_test_bug ("27799");

  if (!org_freedesktop_DBus_GLib_Tests_MyObject_throw_unregistered_error_async (
        f->proxy_for_self, throw_error_cb, f))
    g_error ("Failed to start async ThrowUnregisteredError call");

  while (f->error == NULL)
    g_main_context_iteration (NULL, TRUE);

  g_assert_error (f->error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION);
}

static void
teardown (Fixture *f,
    gconstpointer context G_GNUC_UNUSED)
{
  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->proxy_for_self != NULL)
    {
      g_object_unref (f->proxy_for_self);
      f->proxy_for_self = 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 ("/invalid/gtype", Fixture, NULL, setup, test_invalid_gtype,
      teardown);
  g_test_add ("/invalid/utf8", Fixture, NULL, setup, test_invalid_utf8,
      teardown);
  g_test_add ("/invalid/bool", Fixture, NULL, setup, test_invalid_bool,
      teardown);
  g_test_add ("/invalid/path", Fixture, NULL, setup, test_invalid_path,
      teardown);
  g_test_add ("/invalid/utf8s", Fixture, NULL, setup, test_invalid_utf8s,
      teardown);
  g_test_add ("/invalid/bools", Fixture, NULL, setup, test_invalid_bools,
      teardown);
  g_test_add ("/invalid/paths", Fixture, NULL, setup, test_invalid_paths,
      teardown);
  g_test_add ("/invalid/error/out-of-range", Fixture, NULL, setup,
      test_error_out_of_range, teardown);
  g_test_add ("/invalid/error/domain-0", Fixture, NULL, setup,
      test_error_domain_0, teardown);

  return g_test_run ();
}