summaryrefslogtreecommitdiff
path: root/test/core/registrations.c
blob: 35b09fa2e6ec3b4358ced81b71a9caab43f53cec (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* Regression test for object registration and unregistration
 *
 * Copyright © 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright © 2009-2011 Nokia Corporation
 *
 * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later OR MIT
 *
 * In preparation for dbus-glib relicensing (if it ever happens), this file is
 * licensed under (at your option) either the AFL v2.1, the GPL v2 or later,
 * or an MIT/X11-style license:
 *
 * 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.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <config.h>
#undef G_DISABLE_ASSERT

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include "my-object.h"

#include "dbus-gmain/tests/util.h"

GMainLoop *loop = NULL;

typedef struct {
    DBusError dbus_error;
    DBusGConnection *bus;
    DBusGConnection *bus2;
    GObject *object;
    DBusMessage *frobnicate1_message;
    DBusMessage *frobnicate2_message;
    gboolean received_objectified;
} Fixture;

#define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
static void
_assert_no_error (const DBusError *e,
    const char *file,
    int line)
{
  if (G_UNLIKELY (dbus_error_is_set (e)))
    g_error ("%s:%d: expected success but got error: %s: %s",
        file, line, e->name, e->message);
}

static void
setup (Fixture *f,
    gconstpointer path_to_use)
{
  dbus_error_init (&f->dbus_error);

  f->bus = dbus_g_bus_get_private (DBUS_BUS_SESSION, NULL, NULL);
  g_assert_nonnull (f->bus);

  f->bus2 = dbus_g_bus_get_private (DBUS_BUS_SESSION, NULL, NULL);
  g_assert_nonnull (f->bus2);

  f->object = g_object_new (MY_TYPE_OBJECT, NULL);
  g_assert_true (MY_IS_OBJECT (f->object));
}

static void
teardown (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  /* we close the connection before releasing the object, to test fd.o #5688
   * in test_lookup() */
  if (f->bus != NULL)
    {
      test_run_until_disconnected (dbus_g_connection_get_connection (f->bus), NULL);
      dbus_g_connection_unref (f->bus);
    }

  if (f->bus2 != NULL)
    {
      test_run_until_disconnected (dbus_g_connection_get_connection (f->bus2), NULL);
      dbus_g_connection_unref (f->bus2);
    }

  if (f->object != NULL)
    {
      g_object_unref (f->object);
    }

  /* This is safe to call on an initialized-but-unset DBusError, a bit like
   * g_clear_error */
  dbus_error_free (&f->dbus_error);

  dbus_shutdown ();
}

static void
test_lookup (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  /* teardown() closes the connection before f->object is destroyed, which
   * used to be broken */
  g_test_bug ("5688");

  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);
  /* this was briefly broken while fixing fd.o#5688 */
  g_assert_null (dbus_g_connection_lookup_g_object (f->bus, "/bar"));
}

static void
test_unregister (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  /* feature test: objects can be unregistered */
  g_test_bug ("21219");

  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);
  dbus_g_connection_unregister_g_object (f->bus, f->object);
  g_assert_null (dbus_g_connection_lookup_g_object (f->bus, "/foo"));
}

static void
test_unregister_on_last_unref (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  gpointer weak_pointer;

  weak_pointer = f->object;
  g_object_add_weak_pointer (weak_pointer, &weak_pointer);

  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);
  /* implicit unregistration by the last-unref of the object */
  g_object_unref (f->object);
  f->object = NULL;

  g_assert_null (weak_pointer);

  g_assert_null (dbus_g_connection_lookup_g_object (f->bus, "/foo"));
}

static void
test_unregister_on_forced_dispose (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);
  /* implicit unregistration by dispose() of the object (don't try
   * this at home) */
  g_object_run_dispose (f->object);

  g_assert_null (dbus_g_connection_lookup_g_object (f->bus, "/foo"));
}

static void
test_reregister (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);

  /* Before 0.82, re-registering the same object path was leaky but successful.
   * 0.82 disallowed this behaviour. Since 0.84 it was meant to be allowed
   * again, and a no-op, but it actually had the effect of removing all
   * record of the registrations (while leaving the object registered with
   * libdbus). */
  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);

  /* This would critical in 0.84. */
  dbus_g_connection_unregister_g_object (f->bus, f->object);
  g_assert_null (dbus_g_connection_lookup_g_object (f->bus, "/foo"));
}

static DBusHandlerResult
frobnicate_cb (DBusConnection *conn,
    DBusMessage *message,
    void *user_data)
{
  Fixture *f = user_data;

  if (dbus_message_is_signal (message,
        "org.freedesktop.DBus.GLib.Tests.MyObject", "Frobnicate"))
    {
      const char *sender = dbus_message_get_sender (message);
      const char *path = dbus_message_get_path (message);

      g_assert_nonnull (sender);
      g_assert_nonnull (path);

      if (g_strcmp0 (path, "/foo") == 0)
        {
          g_assert_cmpstr (sender, ==, dbus_bus_get_unique_name (
                dbus_g_connection_get_connection (f->bus)));

          g_assert_null (f->frobnicate1_message);
          f->frobnicate1_message = dbus_message_ref (message);
        }
      else
        {
          g_assert_cmpstr (path, ==, "/bar");
          g_assert_cmpstr (sender, ==, dbus_bus_get_unique_name (
                dbus_g_connection_get_connection (f->bus2)));

          g_assert_null (f->frobnicate2_message);
          f->frobnicate2_message = dbus_message_ref (message);
        }
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static void
test_twice (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  dbus_bool_t mem;

  g_test_bug ("32087");

  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);

  dbus_g_connection_register_g_object (f->bus2, "/bar", f->object);

  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus2, "/bar") ==
      f->object);

  dbus_bus_add_match (dbus_g_connection_get_connection (f->bus),
      "type='signal'", &f->dbus_error);
  assert_no_error (&f->dbus_error);
  mem = dbus_connection_add_filter (dbus_g_connection_get_connection (f->bus),
      frobnicate_cb, f, NULL);
  g_assert_true (mem);

  my_object_emit_frobnicate ((MyObject *) f->object, NULL);

  /* The object should emit the signal once onto each connection,
   * from the appropriate location */
  while (f->frobnicate1_message == NULL || f->frobnicate2_message == NULL)
    g_main_context_iteration (NULL, TRUE);

  dbus_message_unref (f->frobnicate1_message);
  f->frobnicate1_message = NULL;
  dbus_message_unref (f->frobnicate2_message);
  f->frobnicate2_message = NULL;

  /* try again, to catch any extra emissions, but first unregister one of the
   * object's locations */
  dbus_g_connection_unregister_g_object (f->bus, f->object);
  my_object_emit_frobnicate ((MyObject *) f->object, NULL);

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

  g_assert_null (f->frobnicate1_message);
  dbus_message_unref (f->frobnicate2_message);
  f->frobnicate2_message = NULL;
}

static void
test_clean_slate (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  dbus_bool_t mem;

  dbus_bus_add_match (dbus_g_connection_get_connection (f->bus),
      "type='signal'", &f->dbus_error);
  assert_no_error (&f->dbus_error);
  mem = dbus_connection_add_filter (dbus_g_connection_get_connection (f->bus),
      frobnicate_cb, f, NULL);
  g_assert_true (mem);

  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);

  my_object_emit_frobnicate ((MyObject *) f->object, NULL);

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

  dbus_message_unref (f->frobnicate1_message);
  f->frobnicate1_message = NULL;

  /* unregister the object from its last object path, then put it back
   * in the same location */
  dbus_g_connection_unregister_g_object (f->bus, f->object);
  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);

  /* bug: in 0.92, this would be emitted twice because the hook was added
   * twice */
  my_object_emit_frobnicate ((MyObject *) f->object, NULL);

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

  dbus_message_unref (f->frobnicate1_message);
  f->frobnicate1_message = NULL;

  /* unregister the object from its last object path, then put it back
   * at a different location */
  dbus_g_connection_unregister_g_object (f->bus, f->object);
  dbus_g_connection_register_g_object (f->bus2, "/bar", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus2, "/bar") ==
      f->object);

  my_object_emit_frobnicate ((MyObject *) f->object, NULL);

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

  /* check that this wasn't received anyway, which would indicate that
   * either unregistration from /foo was unsuccessful, or the double
   * emission mentioned above was seen */
  g_assert_null (f->frobnicate1_message);

  dbus_message_unref (f->frobnicate2_message);
  f->frobnicate2_message = NULL;
}

static DBusHandlerResult
objectified_cb (DBusConnection *conn,
    DBusMessage *message,
    void *user_data)
{
  Fixture *f = user_data;

  if (dbus_message_is_signal (message,
        "org.freedesktop.DBus.GLib.Tests.MyObject", "Objectified"))
    {
      const char *sender = dbus_message_get_sender (message);
      const char *path = dbus_message_get_path (message);
      dbus_bool_t ok;
      DBusError e;

      dbus_error_init (&e);

      g_assert_nonnull (sender);
      g_assert_nonnull (path);

      g_assert_cmpstr (path, ==, "/foo");
      g_assert_cmpstr (sender, ==, dbus_bus_get_unique_name (
            dbus_g_connection_get_connection (f->bus)));

      path = NULL;
      ok = dbus_message_get_args (message, &e,
          DBUS_TYPE_OBJECT_PATH, &path,
          DBUS_TYPE_INVALID);

      if (dbus_error_is_set (&e))
        g_error ("%s: %s", e.name, e.message);

      g_assert_true (ok);
      g_assert_cmpstr (path, ==, "/foo");

      f->received_objectified = TRUE;
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static void
test_marshal_object (Fixture *f,
    gconstpointer test_data G_GNUC_UNUSED)
{
  dbus_bool_t mem;

  g_test_bug ("37852");

  dbus_g_connection_register_g_object (f->bus, "/foo", f->object);
  g_assert_true (dbus_g_connection_lookup_g_object (f->bus, "/foo") ==
      f->object);

  dbus_bus_add_match (dbus_g_connection_get_connection (f->bus),
      "type='signal'", &f->dbus_error);
  assert_no_error (&f->dbus_error);
  mem = dbus_connection_add_filter (dbus_g_connection_get_connection (f->bus),
      objectified_cb, f, NULL);
  g_assert_true (mem);

  my_object_emit_objectified ((MyObject *) f->object, f->object);

  while (!f->received_objectified)
    g_main_context_iteration (NULL, TRUE);
}

int
main (int argc, char **argv)
{
  g_type_init ();
  g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
  dbus_g_type_specialized_init ();
  g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
  g_test_init (&argc, &argv, NULL);

  g_test_add ("/registrations/lookup", Fixture, NULL,
      setup, test_lookup, teardown);
  g_test_add ("/registrations/unregister", Fixture, NULL,
      setup, test_unregister, teardown);
  g_test_add ("/registrations/unregister-on-last-unref", Fixture, NULL,
      setup, test_unregister_on_last_unref, teardown);
  g_test_add ("/registrations/unregister-on-forced-dispose", Fixture, NULL,
      setup, test_unregister_on_forced_dispose, teardown);
  g_test_add ("/registrations/reregister", Fixture, NULL,
      setup, test_reregister, teardown);
  g_test_add ("/registrations/twice", Fixture, NULL,
      setup, test_twice, teardown);
  g_test_add ("/registrations/clean-slate", Fixture, NULL,
      setup, test_clean_slate, teardown);
  g_test_add ("/registrations/marshal-object", Fixture, NULL,
      setup, test_marshal_object, teardown);

  return g_test_run ();
}