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
|
/* Feature test for the user's self-handle/self-contact changing.
*
* Copyright (C) 2009-2010 Collabora Ltd. <http://www.collabora.co.uk/>
* Copyright (C) 2009 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.
*/
#include "config.h"
#include <telepathy-glib/connection.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include "tests/lib/contacts-conn.h"
#include "tests/lib/debug.h"
#include "tests/lib/myassert.h"
#include "tests/lib/util.h"
typedef struct {
TpDBusDaemon *dbus;
TpTestsSimpleConnection *service_conn;
TpBaseConnection *service_conn_as_base;
gchar *name;
gchar *conn_path;
GError *error /* zero-initialized */;
TpConnection *client_conn;
TpHandleRepoIface *contact_repo;
GAsyncResult *result;
} Fixture;
static void
setup (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
gboolean ok;
f->dbus = tp_tests_dbus_daemon_dup_or_die ();
f->service_conn = TP_TESTS_SIMPLE_CONNECTION (
tp_tests_object_new_static_class (TP_TESTS_TYPE_SIMPLE_CONNECTION,
"account", "me@example.com",
"protocol", "simple",
"break-0192-properties", TRUE,
NULL));
f->service_conn_as_base = TP_BASE_CONNECTION (f->service_conn);
g_object_ref (f->service_conn_as_base);
g_assert (f->service_conn != NULL);
g_assert (f->service_conn_as_base != NULL);
f->contact_repo = tp_base_connection_get_handles (f->service_conn_as_base,
TP_HANDLE_TYPE_CONTACT);
ok = tp_base_connection_register (f->service_conn_as_base, "simple",
&f->name, &f->conn_path, &f->error);
g_assert_no_error (f->error);
g_assert (ok);
f->client_conn = tp_connection_new (f->dbus, f->name, f->conn_path,
&f->error);
g_assert_no_error (f->error);
g_assert (f->client_conn != NULL);
}
static void
setup_and_connect (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
gboolean ok;
setup (f, unused);
ok = tp_connection_run_until_ready (f->client_conn, TRUE, &f->error, NULL);
g_assert_no_error (f->error);
g_assert (ok);
}
/* we'll get more arguments, but just ignore them */
static void
swapped_counter_cb (gpointer user_data)
{
guint *times = user_data;
++*times;
}
static void
test_self_handle (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
TpHandle handle;
TpContact *before, *after;
guint handle_times = 0, contact_times = 0;
g_signal_connect_swapped (f->client_conn, "notify::self-handle",
G_CALLBACK (swapped_counter_cb), &handle_times);
g_signal_connect_swapped (f->client_conn, "notify::self-contact",
G_CALLBACK (swapped_counter_cb), &contact_times);
g_assert_cmpstr (tp_handle_inspect (f->contact_repo,
tp_base_connection_get_self_handle (f->service_conn_as_base)), ==,
"me@example.com");
g_assert_cmpuint (tp_connection_get_self_handle (f->client_conn), ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_object_get (f->client_conn,
"self-handle", &handle,
"self-contact", &before,
NULL);
g_assert_cmpuint (handle, ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_assert_cmpuint (tp_contact_get_handle (before), ==, handle);
g_assert_cmpstr (tp_contact_get_identifier (before), ==, "me@example.com");
g_assert_cmpuint (handle_times, ==, 0);
g_assert_cmpuint (contact_times, ==, 0);
/* similar to /nick in IRC */
tp_tests_simple_connection_set_identifier (f->service_conn,
"myself@example.org");
tp_tests_proxy_run_until_dbus_queue_processed (f->client_conn);
while (handle_times < 1 || contact_times < 1)
g_main_context_iteration (NULL, TRUE);
g_assert_cmpuint (handle_times, ==, 1);
g_assert_cmpuint (contact_times, ==, 1);
g_assert_cmpstr (tp_handle_inspect (f->contact_repo,
tp_base_connection_get_self_handle (f->service_conn_as_base)), ==,
"myself@example.org");
g_assert_cmpuint (tp_connection_get_self_handle (f->client_conn), ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_object_get (f->client_conn,
"self-handle", &handle,
"self-contact", &after,
NULL);
g_assert (before != after);
g_assert_cmpuint (handle, ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_assert_cmpuint (tp_contact_get_handle (after), ==, handle);
g_assert_cmpstr (tp_contact_get_identifier (after), ==,
"myself@example.org");
g_object_unref (before);
g_object_unref (after);
}
static void
test_change_early (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
TpHandle handle;
TpContact *after;
guint handle_times = 0, contact_times = 0;
gboolean ok;
GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
g_signal_connect_swapped (f->client_conn, "notify::self-handle",
G_CALLBACK (swapped_counter_cb), &handle_times);
g_signal_connect_swapped (f->client_conn, "notify::self-contact",
G_CALLBACK (swapped_counter_cb), &contact_times);
tp_proxy_prepare_async (f->client_conn, features, tp_tests_result_ready_cb,
&f->result);
g_assert (f->result == NULL);
/* act as though someone else called Connect; emit signals in quick
* succession, so that by the time the TpConnection tries to investigate
* the self-handle, it has already changed */
tp_base_connection_change_status (f->service_conn_as_base,
TP_CONNECTION_STATUS_CONNECTING,
TP_CONNECTION_STATUS_REASON_REQUESTED);
tp_tests_simple_connection_set_identifier (f->service_conn,
"me@example.com");
g_assert_cmpstr (tp_handle_inspect (f->contact_repo,
tp_base_connection_get_self_handle (f->service_conn_as_base)), ==,
"me@example.com");
tp_base_connection_change_status (f->service_conn_as_base,
TP_CONNECTION_STATUS_CONNECTED,
TP_CONNECTION_STATUS_REASON_REQUESTED);
tp_tests_simple_connection_set_identifier (f->service_conn,
"myself@example.org");
g_assert_cmpstr (tp_handle_inspect (f->contact_repo,
tp_base_connection_get_self_handle (f->service_conn_as_base)), ==,
"myself@example.org");
/* now run the main loop and let the client catch up */
tp_tests_run_until_result (&f->result);
ok = tp_proxy_prepare_finish (f->client_conn, f->result, &f->error);
g_assert_no_error (f->error);
g_assert (ok);
/* the self-handle and self-contact change once during connection */
g_assert_cmpuint (handle_times, ==, 1);
g_assert_cmpuint (contact_times, ==, 1);
g_assert_cmpuint (tp_connection_get_self_handle (f->client_conn), ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_object_get (f->client_conn,
"self-handle", &handle,
"self-contact", &after,
NULL);
g_assert_cmpuint (handle, ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_assert_cmpuint (tp_contact_get_handle (after), ==, handle);
g_assert_cmpstr (tp_contact_get_identifier (after), ==,
"myself@example.org");
g_object_unref (after);
}
static void
test_change_inconveniently (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
TpHandle handle;
TpContact *after;
guint handle_times = 0, contact_times = 0, got_self_handle_times = 0;
gboolean ok;
GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
g_signal_connect_swapped (f->client_conn, "notify::self-handle",
G_CALLBACK (swapped_counter_cb), &handle_times);
g_signal_connect_swapped (f->client_conn, "notify::self-contact",
G_CALLBACK (swapped_counter_cb), &contact_times);
g_signal_connect_swapped (f->service_conn, "got-self-handle",
G_CALLBACK (swapped_counter_cb), &got_self_handle_times);
tp_proxy_prepare_async (f->client_conn, features, tp_tests_result_ready_cb,
&f->result);
g_assert (f->result == NULL);
/* act as though someone else called Connect */
tp_base_connection_change_status (f->service_conn_as_base,
TP_CONNECTION_STATUS_CONNECTING,
TP_CONNECTION_STATUS_REASON_REQUESTED);
tp_tests_simple_connection_set_identifier (f->service_conn,
"me@example.com");
g_assert_cmpstr (tp_handle_inspect (f->contact_repo,
tp_base_connection_get_self_handle (f->service_conn_as_base)), ==,
"me@example.com");
tp_base_connection_change_status (f->service_conn_as_base,
TP_CONNECTION_STATUS_CONNECTED,
TP_CONNECTION_STATUS_REASON_REQUESTED);
/* run the main loop until just after GetSelfHandle is processed, to make
* sure the client first saw the old self handle */
while (got_self_handle_times == 0)
g_main_context_iteration (NULL, TRUE);
tp_tests_simple_connection_set_identifier (f->service_conn,
"myself@example.org");
g_assert_cmpstr (tp_handle_inspect (f->contact_repo,
tp_base_connection_get_self_handle (f->service_conn_as_base)), ==,
"myself@example.org");
/* now run the main loop and let the client catch up */
tp_tests_run_until_result (&f->result);
ok = tp_proxy_prepare_finish (f->client_conn, f->result, &f->error);
g_assert_no_error (f->error);
g_assert (ok);
/* the self-handle and self-contact change once during connection */
g_assert_cmpuint (handle_times, ==, 1);
g_assert_cmpuint (contact_times, ==, 1);
g_assert_cmpuint (tp_connection_get_self_handle (f->client_conn), ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_object_get (f->client_conn,
"self-handle", &handle,
"self-contact", &after,
NULL);
g_assert_cmpuint (handle, ==,
tp_base_connection_get_self_handle (f->service_conn_as_base));
g_assert_cmpuint (tp_contact_get_handle (after), ==, handle);
g_assert_cmpstr (tp_contact_get_identifier (after), ==,
"myself@example.org");
g_object_unref (after);
}
static void
test_self_handle_fails (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
gboolean ok;
tp_proxy_prepare_async (f->client_conn, features, tp_tests_result_ready_cb,
&f->result);
g_assert (f->result == NULL);
tp_tests_simple_connection_set_identifier (f->service_conn,
"me@example.com");
tp_tests_simple_connection_set_get_self_handle_error (f->service_conn,
TP_ERROR, TP_ERROR_CONFUSED, "totally wasted");
tp_base_connection_change_status (f->service_conn_as_base,
TP_CONNECTION_STATUS_CONNECTED,
TP_CONNECTION_STATUS_REASON_REQUESTED);
/* now run the main loop and let the client catch up */
tp_tests_run_until_result (&f->result);
ok = tp_proxy_prepare_finish (f->client_conn, f->result, &f->error);
g_assert_error (f->error, TP_ERROR, TP_ERROR_CONFUSED);
g_assert (!ok);
g_clear_error (&f->error);
g_assert_error (tp_proxy_get_invalidated (f->client_conn), TP_ERROR,
TP_ERROR_CONFUSED);
/* don't want to Disconnect during teardown - it'll just fail */
tp_tests_simple_connection_inject_disconnect (f->service_conn);
tp_clear_object (&f->client_conn);
}
static void
teardown (Fixture *f,
gconstpointer unused G_GNUC_UNUSED)
{
g_clear_error (&f->error);
if (f->client_conn != NULL)
tp_tests_connection_assert_disconnect_succeeds (f->client_conn);
tp_clear_object (&f->result);
tp_clear_object (&f->client_conn);
tp_clear_object (&f->service_conn_as_base);
tp_clear_object (&f->service_conn);
tp_clear_pointer (&f->name, g_free);
tp_clear_pointer (&f->conn_path, g_free);
tp_clear_object (&f->dbus);
}
int
main (int argc,
char **argv)
{
tp_tests_init (&argc, &argv);
g_set_prgname ("self-handle");
g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");
g_test_add ("/self-handle", Fixture, NULL, setup_and_connect,
test_self_handle, teardown);
g_test_add ("/self-handle/change-early", Fixture, NULL, setup,
test_change_early, teardown);
g_test_add ("/self-handle/change-inconveniently", Fixture, NULL, setup,
test_change_inconveniently, teardown);
g_test_add ("/self-handle/fails", Fixture, NULL, setup,
test_self_handle_fails, teardown);
return g_test_run ();
}
|