summaryrefslogtreecommitdiff
path: root/tests/dbus/dbus-tube.c
blob: a8ff9f9e05c6d43ddefd04e1c4e18118da5da567 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* Tests of TpDbusTubeChannel
 *
 * Copyright © 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "config.h"

#include <string.h>

#include <telepathy-glib/asv.h>
#include <telepathy-glib/dbus-tube-channel.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/defs.h>
#include <telepathy-glib/dbus.h>

#include "tests/lib/util.h"
#include "tests/lib/contacts-conn.h"
#include "tests/lib/dbus-tube-chan.h"

typedef struct {
    GMainLoop *mainloop;
    GDBusConnection *dbus;

    /* Service side objects */
    TpBaseConnection *base_connection;
    TpTestsDBusTubeChannel *tube_chan_service;
    TpHandleRepoIface *contact_repo;
    TpHandleRepoIface *room_repo;

    /* Client side objects */
    TpConnection *connection;
    TpDBusTubeChannel *tube;

    GDBusConnection *tube_conn;
    GDBusConnection *cm_conn;
    GVariant *call_result;

    GError *error /* initialized where needed */;
    gint wait;
} Test;

static void
setup (Test *test,
       gconstpointer data)
{
  test->mainloop = g_main_loop_new (NULL, FALSE);
  test->dbus = tp_tests_dbus_dup_or_die ();

  test->error = NULL;

  /* Create (service and client sides) connection objects */
  tp_tests_create_and_connect_conn (TP_TESTS_TYPE_CONTACTS_CONNECTION,
      "me@test.com", &test->base_connection, &test->connection);
}

static void
teardown (Test *test,
          gconstpointer data)
{
  g_clear_error (&test->error);

  tp_clear_object (&test->dbus);
  g_main_loop_unref (test->mainloop);
  test->mainloop = NULL;

  tp_clear_object (&test->tube_chan_service);
  tp_clear_object (&test->tube);

  tp_tests_connection_assert_disconnect_succeeds (test->connection);
  g_object_unref (test->connection);
  g_object_unref (test->base_connection);

  g_clear_object (&test->tube_conn);
  g_clear_object (&test->cm_conn);
  tp_clear_pointer (&test->call_result, g_variant_unref);
}

static void
create_tube_service (Test *test,
    gboolean requested,
    gboolean contact)
{
  gchar *chan_path;
  TpHandle handle, alf_handle;
  GVariant *props;
  GType type;
  TpClientFactory *factory;

  /* If previous tube is still preparing, refs are kept on it. We want it to be
   * destroyed now otherwise it will get reused by the factory. */
  if (test->tube != NULL)
    tp_tests_proxy_run_until_prepared (test->tube, NULL);

  tp_clear_object (&test->tube_chan_service);
  tp_clear_object (&test->tube);

  /* Create service-side tube channel object */
  chan_path = g_strdup_printf ("%s/Channel",
      tp_proxy_get_object_path (test->connection));

  test->contact_repo = tp_base_connection_get_handles (test->base_connection,
      TP_ENTITY_TYPE_CONTACT);
  g_assert (test->contact_repo != NULL);

  test->room_repo = tp_base_connection_get_handles (test->base_connection,
      TP_ENTITY_TYPE_ROOM);
  g_assert (test->room_repo != NULL);

  if (contact)
    {
      handle = tp_handle_ensure (test->contact_repo, "bob", NULL, &test->error);
      type = TP_TESTS_TYPE_CONTACT_DBUS_TUBE_CHANNEL;
    }
  else
    {
      handle = tp_handle_ensure (test->room_repo, "#test", NULL, &test->error);
      type = TP_TESTS_TYPE_ROOM_DBUS_TUBE_CHANNEL;
    }

  g_assert_no_error (test->error);

  alf_handle = tp_handle_ensure (test->contact_repo, "alf", NULL, &test->error);
  g_assert_no_error (test->error);

  test->tube_chan_service = g_object_new (
      type,
      "connection", test->base_connection,
      "handle", handle,
      "requested", requested,
      "object-path", chan_path,
      "initiator-handle", alf_handle,
      NULL);

  /* Create client-side tube channel object */
  g_object_get (test->tube_chan_service,
      "channel-properties", &props,
      NULL);

  factory = tp_proxy_get_factory (test->connection);
  test->tube = (TpDBusTubeChannel *) tp_client_factory_ensure_channel (
      factory, test->connection, chan_path, props,
      &test->error);
  g_assert (TP_IS_DBUS_TUBE_CHANNEL (test->tube));

  g_assert_no_error (test->error);

  g_free (chan_path);
  g_variant_unref (props);
}

/* Test Basis */

static void
test_creation (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  create_tube_service (test, TRUE, TRUE);

  g_assert (TP_IS_DBUS_TUBE_CHANNEL (test->tube));
  g_assert (TP_IS_CHANNEL (test->tube));

  create_tube_service (test, FALSE, FALSE);

  g_assert (TP_IS_DBUS_TUBE_CHANNEL (test->tube));
  g_assert (TP_IS_CHANNEL (test->tube));
}

static void
check_parameters_vardict (GVariant *parameters_vardict)
{
  guint32 badger_value;

  g_assert (parameters_vardict != NULL);

  g_assert (g_variant_lookup (parameters_vardict, "badger",
      "u", &badger_value));
  g_assert_cmpuint (badger_value, ==, 42);
}

static void
test_properties (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  gchar *service;
  GVariant *parameters_vardict;

  /* Outgoing tube */
  create_tube_service (test, TRUE, TRUE);

  /* Service */
  g_assert_cmpstr (tp_dbus_tube_channel_get_service_name (test->tube), ==,
      "com.test.Test");
  g_object_get (test->tube, "service-name", &service, NULL);
  g_assert_cmpstr (service, ==, "com.test.Test");
  g_free (service);

  /* Parameters */
  parameters_vardict = tp_dbus_tube_channel_dup_parameters (
      test->tube);
  /* NULL as the tube has not be offered yet */
  g_assert (parameters_vardict == NULL);

  /* Incoming tube */
  create_tube_service (test, FALSE, FALSE);

  /* Parameters */
  parameters_vardict = tp_dbus_tube_channel_dup_parameters (
      test->tube);
  check_parameters_vardict (parameters_vardict);

  g_variant_unref (parameters_vardict);

  g_object_get (test->tube,
      "parameters", &parameters_vardict,
      NULL);
  check_parameters_vardict (parameters_vardict);
  g_variant_unref (parameters_vardict);
}

static void
tube_offer_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  Test *test = user_data;

  g_clear_object (&test->tube_conn);

  test->tube_conn = tp_dbus_tube_channel_offer_finish (
      TP_DBUS_TUBE_CHANNEL (source), result, &test->error);

  test->wait--;
  if (test->wait <= 0)
    g_main_loop_quit (test->mainloop);
}

static gboolean
new_connection_cb (TpTestsDBusTubeChannel *chan,
    GDBusConnection *connection,
    Test *test)
{
  g_clear_object (&test->cm_conn);
  test->cm_conn = g_object_ref (connection);

  test->wait--;
  if (test->wait <= 0)
    g_main_loop_quit (test->mainloop);

  return TRUE;
}

static void
handle_double_call (GDBusConnection       *connection,
    const gchar *sender,
    const gchar *object_path,
    const gchar *interface_name,
    const gchar *method_name,
    GVariant *parameters,
    GDBusMethodInvocation *invocation,
    gpointer user_data)
{
  if (!tp_strdiff (method_name, "Double"))
    {
      gint value;

      g_variant_get (parameters, "(i)", &value);

      g_dbus_method_invocation_return_value (invocation,
                                             g_variant_new ("(i)", value * 2));
    }
}

static void
register_object (GDBusConnection *connection)
{
  GDBusNodeInfo *introspection_data;
  guint registration_id;
  static const GDBusInterfaceVTable interface_vtable =
  {
    handle_double_call,
    NULL,
    NULL,
  };
  static const gchar introspection_xml[] =
    "<node>"
    "  <interface name='org.Example.TestInterface'>"
    "    <method name='Double'>"
    "      <arg type='i' name='value' direction='in'/>"
    "      <arg type='i' name='result' direction='out'/>"
    "    </method>"
    "  </interface>"
    "</node>";

  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
  g_assert (introspection_data != NULL);

  registration_id = g_dbus_connection_register_object (connection,
      "/org/Example/TestObject", introspection_data->interfaces[0],
      &interface_vtable, NULL, NULL, NULL);
  g_assert (registration_id > 0);

  g_dbus_node_info_unref (introspection_data);
}

static void
double_call_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  Test *test = user_data;

  tp_clear_pointer (&test->call_result, g_variant_unref);

  test->call_result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
      result, &test->error);

  test->wait--;
  if (test->wait <= 0)
    g_main_loop_quit (test->mainloop);
}

static void
use_tube (Test *test,
    GDBusConnection *server_conn,
    GDBusConnection *client_conn)
{
  gint result;

  /* Server publishes an object on the tube */
  register_object (server_conn);

  /* Client calls a remote method */
  g_dbus_connection_call (client_conn, NULL, "/org/Example/TestObject",
      "org.Example.TestInterface", "Double",
      g_variant_new ("(i)", 42),
      G_VARIANT_TYPE ("(i)"), G_DBUS_CALL_FLAGS_NONE, -1,
      NULL, double_call_cb, test);

  test->wait = 1;
  g_main_loop_run (test->mainloop);
  g_assert_no_error (test->error);

  g_variant_get (test->call_result, "(i)", &result);
  g_assert_cmpuint (result, ==, 42 * 2);
}

static void
test_offer (Test *test,
    gconstpointer data)
{
  const TpTestsDBusTubeChannelOpenMode open_mode = GPOINTER_TO_UINT (data);
  GVariantDict params;
  GVariant *variant;

  /* Outgoing tube */
  create_tube_service (test, TRUE, TRUE);
  tp_tests_dbus_tube_channel_set_open_mode (test->tube_chan_service, open_mode);

  g_variant_dict_init (&params, NULL);
  g_variant_dict_insert (&params, "badger", "u", 42);

  g_signal_connect (test->tube_chan_service, "new-connection",
      G_CALLBACK (new_connection_cb), test);

  tp_dbus_tube_channel_offer_async (test->tube, g_variant_dict_end (&params),
      tube_offer_cb, test);

  test->wait = 2;
  g_main_loop_run (test->mainloop);
  g_assert_no_error (test->error);

  variant = tp_dbus_tube_channel_dup_parameters (test->tube);
  check_parameters_vardict (variant);
  g_variant_unref (variant);

  g_assert (G_IS_DBUS_CONNECTION (test->tube_conn));
  g_assert (G_IS_DBUS_CONNECTION (test->cm_conn));

  use_tube (test, test->tube_conn, test->cm_conn);
}

static void
test_offer_invalidated_before_open (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  /* Outgoing tube */
  create_tube_service (test, TRUE, TRUE);
  tp_tests_dbus_tube_channel_set_open_mode (test->tube_chan_service,
      TP_TESTS_DBUS_TUBE_CHANNEL_NEVER_OPEN);

  tp_dbus_tube_channel_offer_async (test->tube, NULL, tube_offer_cb, test);

  test->wait = 1;
  g_main_loop_run (test->mainloop);
  /* FIXME: this isn't a particularly good error… it's just what comes out when
   * the channel gets closed from under us, and there isn't really API on
   * DBusTube to give a better error.
   *
   * https://bugs.freedesktop.org/show_bug.cgi?id=48196
   */
  g_assert_error (test->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_OBJECT_REMOVED);
}

static void
tube_accept_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  Test *test = user_data;

  g_clear_object (&test->tube_conn);

  test->tube_conn = tp_dbus_tube_channel_accept_finish (
      TP_DBUS_TUBE_CHANNEL (source), result, &test->error);

  test->wait--;
  if (test->wait <= 0)
    g_main_loop_quit (test->mainloop);
}

static void
test_accept (Test *test,
    gconstpointer data)
{
  const TpTestsDBusTubeChannelOpenMode open_mode = GPOINTER_TO_UINT (data);

  /* Incoming tube */
  create_tube_service (test, FALSE, TRUE);
  tp_tests_dbus_tube_channel_set_open_mode (test->tube_chan_service, open_mode);

  g_signal_connect (test->tube_chan_service, "new-connection",
      G_CALLBACK (new_connection_cb), test);

  tp_dbus_tube_channel_accept_async (test->tube, tube_accept_cb, test);

  test->wait = 2;
  g_main_loop_run (test->mainloop);
  g_assert_no_error (test->error);

  g_assert (G_IS_DBUS_CONNECTION (test->tube_conn));
  g_assert (G_IS_DBUS_CONNECTION (test->cm_conn));

  use_tube (test, test->cm_conn, test->tube_conn);
}

static void
test_accept_invalidated_before_open (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  /* Incoming tube */
  create_tube_service (test, FALSE, TRUE);
  tp_tests_dbus_tube_channel_set_open_mode (test->tube_chan_service,
      TP_TESTS_DBUS_TUBE_CHANNEL_NEVER_OPEN);

  tp_dbus_tube_channel_accept_async (test->tube, tube_accept_cb, test);

  test->wait = 1;
  g_main_loop_run (test->mainloop);
  /* FIXME: this isn't a particularly good error… it's just what comes out when
   * the channel gets closed from under us, and there isn't really API on
   * DBusTube to give a better error.
   *
   * https://bugs.freedesktop.org/show_bug.cgi?id=48196
   */
  g_assert_error (test->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_OBJECT_REMOVED);
}

int
main (int argc,
      char **argv)
{
  tp_tests_init (&argc, &argv);
  g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");

  g_test_add ("/dbus-tube/creation", Test, NULL, setup, test_creation,
      teardown);
  g_test_add ("/dbus-tube/properties", Test, NULL, setup, test_properties,
      teardown);
  /* Han shot first. */
  g_test_add ("/dbus-tube/offer-open-first", Test,
      GUINT_TO_POINTER (TP_TESTS_DBUS_TUBE_CHANNEL_OPEN_FIRST),
      setup, test_offer, teardown);
  g_test_add ("/dbus-tube/offer-open-second", Test,
      GUINT_TO_POINTER (TP_TESTS_DBUS_TUBE_CHANNEL_OPEN_SECOND),
      setup, test_offer, teardown);
  g_test_add ("/dbus-tube/offer-invalidated-before-open", Test, NULL,
      setup, test_offer_invalidated_before_open, teardown);
  g_test_add ("/dbus-tube/accept-open-first", Test,
      GUINT_TO_POINTER (TP_TESTS_DBUS_TUBE_CHANNEL_OPEN_FIRST),
      setup, test_accept, teardown);
  g_test_add ("/dbus-tube/accept-open-second", Test,
      GUINT_TO_POINTER (TP_TESTS_DBUS_TUBE_CHANNEL_OPEN_SECOND),
      setup, test_accept, teardown);
  g_test_add ("/dbus-tube/accept-invalidated-before-open", Test, NULL,
      setup, test_accept_invalidated_before_open, teardown);

  return tp_tests_run_with_bus ();
}