summaryrefslogtreecommitdiff
path: root/tests/dbus/simple-handler.c
blob: 0d3ede09bd324c1e26cd0a777a95d59220c76f64 (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
503
504
505
506
/* Tests of TpSimpleHandler
 *
 * 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 <telepathy-glib/simple-handler.h>
#include <telepathy-glib/cli-channel.h>
#include <telepathy-glib/cli-misc.h>
#include <telepathy-glib/client.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/defs.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/proxy-subclass.h>

#include "tests/lib/util.h"
#include "tests/lib/simple-account.h"
#include "tests/lib/contacts-conn.h"
#include "tests/lib/echo-chan.h"

typedef struct {
    GMainLoop *mainloop;
    GDBusConnection *dbus;
    TpClientFactory *factory;

    /* Service side objects */
    TpBaseClient *simple_handler;
    TpBaseConnection *base_connection;
    TpTestsSimpleAccount *account_service;
    TpTestsEchoChannel *text_chan_service;

    /* Client side objects */
    TpClient *client;
    TpConnection *connection;
    TpAccount *account;
    TpChannel *text_chan;

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

#define ACCOUNT_PATH TP_ACCOUNT_OBJECT_PATH_BASE "what/ev/er"

static void
setup (Test *test,
       gconstpointer data)
{
  gchar *chan_path;
  TpHandle handle;
  TpHandleRepoIface *contact_repo;

  test->mainloop = g_main_loop_new (NULL, FALSE);
  test->dbus = tp_tests_dbus_dup_or_die ();
  test->factory = tp_client_factory_dup (NULL);

  test->error = NULL;

  /* Claim AccountManager bus-name (needed as we're going to export an Account
   * object). */
  tp_dbus_daemon_request_name (test->dbus,
          TP_ACCOUNT_MANAGER_BUS_NAME, FALSE, &test->error);
  g_assert_no_error (test->error);

  /* Create service-side Account object */
  test->account_service = tp_tests_object_new_static_class (
      TP_TESTS_TYPE_SIMPLE_ACCOUNT, NULL);
  tp_dbus_daemon_register_object (test->dbus, ACCOUNT_PATH,
      test->account_service);

   /* Create client-side Account object */
  test->account = tp_tests_account_new (test->dbus, ACCOUNT_PATH, NULL);
  g_assert (test->account != 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);

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

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

  handle = tp_handle_ensure (contact_repo, "bob", NULL, &test->error);
  g_assert_no_error (test->error);

  test->text_chan_service = TP_TESTS_ECHO_CHANNEL (
      tp_tests_object_new_static_class (
        TP_TESTS_TYPE_ECHO_CHANNEL,
        "connection", test->base_connection,
        "object-path", chan_path,
        "handle", handle,
        NULL));

  /* Create client-side text channel object */
  test->text_chan = tp_tests_channel_new (test->connection, chan_path, NULL,
      TP_ENTITY_TYPE_CONTACT, handle, &test->error);
  g_assert_no_error (test->error);

  g_free (chan_path);
}

static void
teardown_channel_invalidated_cb (TpChannel *self,
  guint domain,
  gint code,
  gchar *message,
  Test *test)
{
  g_main_loop_quit (test->mainloop);
}

static void
teardown_run_close_channel (Test *test, TpChannel *channel)
{
  if (channel != NULL && tp_proxy_get_invalidated (channel) == NULL)
    {
      g_signal_connect (channel, "invalidated",
          G_CALLBACK (teardown_channel_invalidated_cb), test);
      tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
      g_main_loop_run (test->mainloop);
    }
}

static void
teardown (Test *test,
          gconstpointer data)
{
  teardown_run_close_channel (test, test->text_chan);

  g_clear_error (&test->error);

  g_object_unref (test->simple_handler);
  g_object_unref (test->client);

  tp_dbus_daemon_unregister_object (test->dbus, test->account_service);
  g_object_unref (test->account_service);

  tp_dbus_daemon_release_name (test->dbus, TP_ACCOUNT_MANAGER_BUS_NAME,
      &test->error);
  g_assert_no_error (test->error);

  g_object_unref (test->factory);
  g_object_unref (test->dbus);
  test->dbus = NULL;
  g_main_loop_unref (test->mainloop);
  test->mainloop = NULL;

  g_object_unref (test->account);

  g_object_unref (test->text_chan_service);
  g_object_unref (test->text_chan);

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

static void
create_simple_handler (Test *test,
    gboolean bypass_approval,
    gboolean requests,
    TpSimpleHandlerHandleChannelImpl impl)
{
  /* Create service-side Client object */
  test->simple_handler = tp_tests_object_new_static_class (
      TP_TYPE_SIMPLE_HANDLER,
      "factory", test->factory,
      "bypass-approval", bypass_approval,
      "requests", requests,
      "name", "MySimpleHandler",
      "uniquify-name", FALSE,
      "callback", impl,
      "user-data", test,
      "destroy", NULL,
      NULL);
  g_assert (test->simple_handler != NULL);

 /* Create client-side Client object */
  test->client = tp_tests_object_new_static_class (TP_TYPE_CLIENT,
          "bus-name", tp_base_client_get_bus_name (test->simple_handler),
          "object-path", tp_base_client_get_object_path (test->simple_handler),
          "factory", test->factory,
          NULL);

  g_assert (test->client != NULL);
}

static void
get_client_prop_cb (TpProxy *proxy,
    GHashTable *properties,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  Test *test = user_data;
  const gchar * const *interfaces;

  if (error != NULL)
    {
      test->error = g_error_copy (error);
      goto out;
    }

  g_assert_cmpint (g_hash_table_size (properties), == , 1);

  interfaces = tp_asv_get_strv (properties, "Interfaces");
  g_assert_cmpint (g_strv_length ((GStrv) interfaces), ==, 2);
  g_assert (tp_strv_contains (interfaces, TP_IFACE_CLIENT_HANDLER));
  g_assert (tp_strv_contains (interfaces, TP_IFACE_CLIENT_INTERFACE_REQUESTS));

out:
  g_main_loop_quit (test->mainloop);
}

static void
check_filters (GPtrArray *filters)
{
  GHashTable *filter;

  g_assert (filters != NULL);
  g_assert_cmpuint (filters->len, ==, 2);

  filter = g_ptr_array_index (filters, 0);
  g_assert_cmpuint (g_hash_table_size (filter), ==, 1);
  g_assert_cmpstr (tp_asv_get_string (filter, TP_PROP_CHANNEL_CHANNEL_TYPE), ==,
      TP_IFACE_CHANNEL_TYPE_TEXT);

  filter = g_ptr_array_index (filters, 1);
  g_assert_cmpuint (g_hash_table_size (filter), ==, 2);
  g_assert_cmpstr (tp_asv_get_string (filter, TP_PROP_CHANNEL_CHANNEL_TYPE), ==,
      TP_IFACE_CHANNEL_TYPE_STREAM_TUBE1);
  g_assert_cmpuint (tp_asv_get_uint32 (filter,
        TP_PROP_CHANNEL_TARGET_ENTITY_TYPE, NULL), ==, TP_ENTITY_TYPE_CONTACT);
}

static void
get_handler_prop_cb (TpProxy *proxy,
    GHashTable *properties,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  Test *test = user_data;
  GPtrArray *filters;
  gboolean bypass;
  gboolean valid;
  const gchar * const * capabilities;
  GPtrArray *handled;

  if (error != NULL)
    {
      test->error = g_error_copy (error);
      goto out;
    }

  g_assert_cmpint (g_hash_table_size (properties), == , 4);

  filters = tp_asv_get_boxed (properties, "HandlerChannelFilter",
      TP_ARRAY_TYPE_CHANNEL_CLASS_LIST);
  check_filters (filters);

  bypass = tp_asv_get_boolean (properties, "BypassApproval", &valid);
  g_assert (valid);
  g_assert (!bypass);

  capabilities = tp_asv_get_strv (properties, "Capabilities");
  g_assert_cmpint (g_strv_length ((GStrv) capabilities), ==, 0);

  handled = tp_asv_get_boxed (properties, "HandledChannels",
      TP_ARRAY_TYPE_OBJECT_PATH_LIST);
  g_assert (handled != NULL);
  g_assert_cmpint (handled->len, ==, 0);

out:
  g_main_loop_quit (test->mainloop);
}

static void
handle_channel_success (
    TpSimpleHandler *handler,
    TpAccount *account,
    TpConnection *connection,
    TpChannel *channel,
    GList *requests_satisified,
    gint64 user_action_time,
    TpHandleChannelContext *context,
    gpointer user_data)
{
  GVariant *info;
  guint u = 0;

  info = tp_handle_channel_context_dup_handler_info (context);
  g_assert (info != NULL);
  g_assert (g_variant_is_of_type (info, G_VARIANT_TYPE_VARDICT));
  g_variant_lookup (info, "badger", "u", &u);
  g_assert_cmpuint (u, ==, 42);
  g_variant_unref (info);

  tp_handle_channel_context_accept (context);
}

static void
test_properties (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  create_simple_handler (test, FALSE, TRUE, handle_channel_success);

  tp_base_client_add_handler_filter_variant (test->simple_handler,
      g_variant_new_parsed ("{ %s: <%s> }",
        TP_PROP_CHANNEL_CHANNEL_TYPE, TP_IFACE_CHANNEL_TYPE_TEXT));

  tp_base_client_add_handler_filter_variant (test->simple_handler,
      g_variant_new_parsed ("{ %s: <%s>, %s: <%u> }",
        TP_PROP_CHANNEL_CHANNEL_TYPE, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE1,
        TP_PROP_CHANNEL_TARGET_ENTITY_TYPE, (guint32) TP_ENTITY_TYPE_CONTACT));

  tp_base_client_register (test->simple_handler, &test->error);
  g_assert_no_error (test->error);

  /* Check Client properties */
  tp_cli_dbus_properties_call_get_all (test->client, -1,
      TP_IFACE_CLIENT, get_client_prop_cb, test, NULL, NULL);

  g_main_loop_run (test->mainloop);
  g_assert_no_error (test->error);

  /* Check Handler properties */
  tp_cli_dbus_properties_call_get_all (test->client, -1,
      TP_IFACE_CLIENT_HANDLER, get_handler_prop_cb, test, NULL, NULL);

  g_main_loop_run (test->mainloop);
  g_assert_no_error (test->error);
}

static void
no_return_cb (TpClient *proxy,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  Test *test = user_data;

  g_clear_error (&test->error);

  if (error != NULL)
    {
      test->error = g_error_copy (error);
      goto out;
    }

out:
  g_main_loop_quit (test->mainloop);
}

static void
call_handle_channel (Test *test)
{
  GHashTable *requests_satisfied, *info,* chan_props;
  int i;

  requests_satisfied = g_hash_table_new (NULL, NULL);
  info = tp_asv_new (
      "badger", G_TYPE_UINT, 42,
      NULL);
  chan_props = tp_tests_dup_channel_props_asv (test->text_chan);

  tp_proxy_add_interface_by_id (TP_PROXY (test->client),
      TP_IFACE_QUARK_CLIENT_HANDLER);

  for (i = 0 ; i < 10 ; i ++)
    {
      tp_cli_client_handler_call_handle_channel (test->client, -1,
          tp_proxy_get_object_path (test->account),
          tp_proxy_get_object_path (test->connection),
          tp_proxy_get_object_path (test->text_chan), chan_props,
          requests_satisfied, 0, info,
          no_return_cb, test, NULL, NULL);

      g_main_loop_run (test->mainloop);
    }

  g_hash_table_unref (requests_satisfied);
  g_hash_table_unref (info);
  g_hash_table_unref (chan_props);
}

/* HandleChannel returns immediately */
static void
test_success (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  create_simple_handler (test, FALSE, FALSE, handle_channel_success);

  tp_base_client_add_handler_filter_variant (test->simple_handler,
      g_variant_new_parsed ("@a{sv} {}"));

  tp_base_client_register (test->simple_handler, &test->error);
  g_assert_no_error (test->error);

  call_handle_channel (test);
  g_assert_no_error (test->error);
}

/* HandleChannel returns in an async way */
static gboolean
accept_idle_cb (gpointer data)
{
  TpHandleChannelContext *context = data;

  tp_handle_channel_context_accept (context);
  g_object_unref (context);
  return FALSE;
}

static void
handle_channel_async (
    TpSimpleHandler *handler,
    TpAccount *account,
    TpConnection *connection,
    TpChannel *channel,
    GList *requests_satisified,
    gint64 user_action_time,
    TpHandleChannelContext *context,
    gpointer user_data)
{
  g_idle_add (accept_idle_cb, g_object_ref (context));

  tp_handle_channel_context_delay (context);
}

static void
test_delayed (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  create_simple_handler (test, FALSE, FALSE, handle_channel_async);

  tp_base_client_add_handler_filter_variant (test->simple_handler,
      g_variant_new_parsed ("@a{sv} {}"));

  tp_base_client_register (test->simple_handler, &test->error);
  g_assert_no_error (test->error);

  call_handle_channel (test);
  g_assert_no_error (test->error);
}

/* HandleChannel fails */
static void
handle_channel_fail (
    TpSimpleHandler *handler,
    TpAccount *account,
    TpConnection *connection,
    TpChannel *channel,
    GList *requests_satisified,
    gint64 user_action_time,
    TpHandleChannelContext *context,
    gpointer user_data)
{
  GError error = { TP_ERROR, TP_ERROR_NOT_AVAILABLE,
      "No HandleChannel for you!" };

  tp_handle_channel_context_fail (context, &error);
}

static void
test_fail (Test *test,
    gconstpointer data G_GNUC_UNUSED)
{
  create_simple_handler (test, FALSE, FALSE, handle_channel_fail);

  tp_base_client_add_handler_filter_variant (test->simple_handler,
      g_variant_new_parsed ("@a{sv} {}"));

  tp_base_client_register (test->simple_handler, &test->error);
  g_assert_no_error (test->error);

  call_handle_channel (test);
  g_assert_error (test->error, TP_ERROR, TP_ERROR_NOT_AVAILABLE);
}

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 ("/simple-handler/properties", Test, NULL, setup, test_properties,
      teardown);
  g_test_add ("/simple-handler/success", Test, NULL, setup, test_success,
      teardown);
  g_test_add ("/simple-handler/delayed", Test, NULL, setup, test_delayed,
      teardown);
  g_test_add ("/simple-handler/fail", Test, NULL, setup, test_fail,
      teardown);

  return tp_tests_run_with_bus ();
}