summaryrefslogtreecommitdiff
path: root/tests/dbus/unsupported-interface.c
blob: 9c2978e7a3e8783c9f8937093bf308163cdb25a5 (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
/* Regression test for unsupported interfaces on objects.
 *
 * Copyright © 2007-2012 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright © 2007-2008 Nokia Corporation
 *
 * 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. No warranty.
 */

#include "config.h"

#include <telepathy-glib/cli-connection.h>
#include <telepathy-glib/connection.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/util.h>

#include "telepathy-glib/reentrants.h"

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

typedef struct {
    GDBusConnection *dbus;
    TpTestsSimpleConnection *service_conn;
    TpBaseConnection *service_conn_as_base;
    gchar *conn_name;
    gchar *conn_path;
    TpConnection *conn;

    guint wait;
    gboolean reentrant;
    gboolean freed;
    GError *error /* initialized by GTest */;
} Fixture;

static void
setup (Fixture *f,
    gconstpointer data)
{
  tp_debug_set_flags ("all");
  f->dbus = tp_tests_dbus_dup_or_die ();

  f->service_conn = TP_TESTS_SIMPLE_CONNECTION (
    tp_tests_object_new_static_class (
        TP_TESTS_TYPE_CONTACTS_CONNECTION,
        "account", "me@example.com",
        "protocol", "simple_protocol",
        NULL));
  f->service_conn_as_base = TP_BASE_CONNECTION (f->service_conn);
  g_assert (f->service_conn != NULL);
  g_assert (f->service_conn_as_base != NULL);

  g_assert (tp_base_connection_register (f->service_conn_as_base, "simple",
        &f->conn_name, &f->conn_path, &f->error));
  g_assert_no_error (f->error);

  f->conn = tp_tests_connection_new (f->dbus, f->conn_name, f->conn_path,
      &f->error);
  g_assert_no_error (f->error);
}

static void
test_supported_run (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_cli_connection_run_connect (f->conn, -1, &f->error, NULL);
  g_assert_no_error (f->error);
  g_assert (ok);
}

static void
test_unsupported_run (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_cli_connection_interface_mail_notification1_run_request_inbox_url (
      f->conn, -1, NULL /* "out" arg */, &f->error, NULL);
  g_assert_error (f->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_NO_INTERFACE);
  g_assert (!ok);
}

static void
pretend_to_free (gpointer p)
{
  Fixture *f = p;

  g_assert (!f->freed);
  f->freed = TRUE;
}

static void
connect_cb (TpConnection *conn,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  Fixture *f = user_data;

  g_assert_no_error (f->error);
  g_assert (!f->reentrant);
  g_assert (!f->freed);

  if (error != NULL)
    f->error = g_error_copy (error);

  f->wait--;
}

static void
test_supported_async (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  TpProxyPendingCall *call;

  f->reentrant = TRUE;
  f->wait = 1;
  call = tp_cli_connection_call_connect (f->conn, -1, connect_cb,
      f, pretend_to_free, NULL);
  f->reentrant = FALSE;

  g_assert (call != NULL);
  g_assert (!f->freed);

  while (f->wait)
    g_main_context_iteration (NULL, TRUE);

  g_assert_no_error (f->error);
  g_assert (f->freed);
}

static void
inbox_url_cb (TpConnection *conn,
    const GValueArray *va,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  Fixture *f = user_data;

  g_assert_no_error (f->error);
  g_assert (!f->reentrant);
  g_assert (!f->freed);

  if (error != NULL)
    f->error = g_error_copy (error);

  f->wait--;
}

static void
test_unsupported_async (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  TpProxyPendingCall *call;

  f->reentrant = TRUE;
  f->wait = 1;
  call = tp_cli_connection_interface_mail_notification1_call_request_inbox_url (
      f->conn, -1, inbox_url_cb, f, pretend_to_free, NULL);
  f->reentrant = FALSE;

  g_assert (call != NULL);
  g_assert (!f->freed);

  while (f->wait)
    g_main_context_iteration (NULL, TRUE);

  g_assert_error (f->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_NO_INTERFACE);
  g_assert (f->freed);
}

static void
do_nothing (TpConnection *conn,
    ...)
{
}

static void
test_supported_signal (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  TpProxySignalConnection *sc;

  sc = tp_cli_connection_connect_to_status_changed (f->conn,
      (void (*)(TpConnection *, guint, guint, gpointer, GObject *)) do_nothing,
      f, pretend_to_free, NULL, &f->error);

  g_assert_no_error (f->error);
  g_assert (sc != NULL);
  g_assert (!f->freed);

  tp_proxy_signal_connection_disconnect (sc);

  /* GDBus invokes destructors in an idle */
  while (!f->freed)
    g_main_context_iteration (NULL, TRUE);
}

static void
test_unsupported_signal (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  TpProxySignalConnection *sc;

  sc = tp_cli_connection_interface_mail_notification1_connect_to_mails_received (
      f->conn,
      (void (*)(TpConnection *, const GPtrArray *, gpointer, GObject *)) do_nothing,
      f, pretend_to_free, NULL, &f->error);

  g_assert_error (f->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_NO_INTERFACE);
  g_assert (sc == NULL);
  g_assert (f->freed);
}

static void
teardown (Fixture *f,
    gconstpointer data)
{
  TpConnection *conn;

  g_clear_error (&f->error);

  g_clear_object (&f->conn);

  /* disconnect the connection so we don't leak it */
  conn = tp_tests_connection_new (f->dbus, f->conn_name, f->conn_path,
      &f->error);
  g_assert (conn != NULL);
  g_assert_no_error (f->error);

  tp_tests_connection_assert_disconnect_succeeds (conn);
  tp_tests_proxy_run_until_prepared_or_failed (conn, NULL, &f->error);
  g_assert_error (f->error, TP_ERROR, TP_ERROR_CANCELLED);
  g_clear_error (&f->error);

  g_object_unref (conn);

  /* borrowed from service_conn */
  f->service_conn_as_base = NULL;

  g_clear_object (&f->service_conn);
  g_free (f->conn_name);
  g_free (f->conn_path);

  g_clear_object (&f->dbus);
}

int
main (int argc,
      char **argv)
{
  tp_tests_init (&argc, &argv);

  g_test_add ("/supported/run", Fixture, NULL,
      setup, test_supported_run, teardown);
  g_test_add ("/supported/async", Fixture, NULL,
      setup, test_supported_async, teardown);
  g_test_add ("/supported/signal", Fixture, NULL,
      setup, test_supported_signal, teardown);
  g_test_add ("/unsupported/run", Fixture, NULL,
      setup, test_unsupported_run, teardown);
  g_test_add ("/unsupported/async", Fixture, NULL,
      setup, test_unsupported_async, teardown);
  g_test_add ("/unsupported/signal", Fixture, NULL,
      setup, test_unsupported_signal, teardown);

  return tp_tests_run_with_bus ();
}