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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/* Tests of TpTextChannel
*
* 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/telepathy-glib.h>
#include <telepathy-glib/cli-connection.h>
#include <telepathy-glib/message-mixin.h>
#include "examples/cm/contactlist/conn.h"
#include "tests/lib/util.h"
typedef struct {
GMainLoop *mainloop;
GDBusConnection *dbus;
/* Service side objects */
TpBaseConnection *base_connection;
TpHandleRepoIface *contact_repo;
/* Client side objects */
TpConnection *connection;
TpTextChannel *channel;
TpTextChannel *sms_channel;
GPtrArray *blocked_added;
GPtrArray *blocked_removed;
TpContact *contact;
GError *error /* initialized where needed */;
gint wait;
} Test;
static void
setup (Test *test,
gconstpointer data)
{
gchar *conn_name, *conn_path;
GQuark conn_features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
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 */
test->base_connection = tp_tests_object_new_static_class (
EXAMPLE_TYPE_CONTACT_LIST_CONNECTION,
"account", "me@test.com",
"simulation-delay", 0,
"protocol", "test",
NULL);
g_assert (tp_base_connection_register (test->base_connection, "example",
&conn_name, &conn_path, &test->error));
g_assert_no_error (test->error);
test->connection = tp_tests_connection_new (test->dbus, conn_name, conn_path,
&test->error);
g_assert_no_error (test->error);
test->contact_repo = tp_base_connection_get_handles (test->base_connection,
TP_ENTITY_TYPE_CONTACT);
g_assert (test->contact_repo != NULL);
/* Connect the connection */
tp_cli_connection_call_connect (test->connection, -1, NULL, NULL, NULL, NULL);
tp_tests_proxy_run_until_prepared (test->connection, conn_features);
g_free (conn_name);
g_free (conn_path);
}
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_tests_connection_assert_disconnect_succeeds (test->connection);
g_object_unref (test->connection);
g_object_unref (test->base_connection);
tp_clear_pointer (&test->blocked_added, g_ptr_array_unref);
tp_clear_pointer (&test->blocked_removed, g_ptr_array_unref);
g_clear_object (&test->contact);
}
static void
block_contacts_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
Test *test = user_data;
tp_connection_block_contacts_finish (TP_CONNECTION (source), result,
&test->error);
g_assert_no_error (test->error);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
unblock_contacts_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
Test *test = user_data;
tp_connection_unblock_contacts_finish (TP_CONNECTION (source), result,
&test->error);
g_assert_no_error (test->error);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
contact_block_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
Test *test = user_data;
tp_contact_block_finish (TP_CONTACT (source), result, &test->error);
g_assert_no_error (test->error);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
contact_unblock_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
Test *test = user_data;
tp_contact_unblock_finish (TP_CONTACT (source), result, &test->error);
g_assert_no_error (test->error);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static TpContact *
create_contact (Test *test,
const gchar *id)
{
TpHandle handle;
TpContact *contact;
handle = tp_handle_ensure (test->contact_repo, id, NULL, &test->error);
g_assert_no_error (test->error);
contact = tp_connection_dup_contact_if_possible (test->connection, handle,
id);
g_assert (contact != NULL);
return contact;
}
static void
test_block_unblock (Test *test,
gconstpointer data G_GNUC_UNUSED)
{
TpContact *alice, *bob;
GPtrArray *arr;
alice = create_contact (test, "alice");
bob = create_contact (test, "bob");
arr = g_ptr_array_sized_new (2);
g_ptr_array_add (arr, alice);
g_ptr_array_add (arr, bob);
/* Block contacts */
tp_connection_block_contacts_async (test->connection,
arr->len, (TpContact * const *) arr->pdata, FALSE,
block_contacts_cb, test);
test->wait = 1;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
/* Unblock contacts */
tp_connection_unblock_contacts_async (test->connection,
arr->len, (TpContact * const *) arr->pdata,
unblock_contacts_cb, test);
test->wait = 1;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_object_unref (alice);
g_object_unref (bob);
g_ptr_array_unref (arr);
}
static void
proxy_prepare_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
Test *test = user_data;
tp_proxy_prepare_finish (source, result, &test->error);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
test_can_report_abusive (Test *test,
gconstpointer data G_GNUC_UNUSED)
{
GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_BLOCKING, 0 };
gboolean abuse;
/* Feature is not prepared yet */
g_object_get (test->connection, "can-report-abusive", &abuse, NULL);
g_assert (!abuse);
g_assert (!tp_connection_can_report_abusive (test->connection));
tp_proxy_prepare_async (test->connection, features,
proxy_prepare_cb, test);
test->wait = 1;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_assert (tp_proxy_is_prepared (test->connection,
TP_CONNECTION_FEATURE_CONTACT_BLOCKING));
g_object_get (test->connection, "can-report-abusive", &abuse, NULL);
g_assert (abuse);
g_assert (tp_connection_can_report_abusive (test->connection));
}
static void
blocked_contacts_changed_cb (TpConnection *conn,
GPtrArray *added,
GPtrArray *removed,
Test *test)
{
tp_clear_pointer (&test->blocked_added, g_ptr_array_unref);
tp_clear_pointer (&test->blocked_removed, g_ptr_array_unref);
test->blocked_added = g_ptr_array_ref (added);
test->blocked_removed = g_ptr_array_ref (removed);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
test_blocked_contacts (Test *test,
gconstpointer data G_GNUC_UNUSED)
{
GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_BLOCKING, 0 };
GPtrArray *blocked;
TpContact *alice, *bill, *guillaume, *sjoerd, *steve;
gboolean use_contact_api = GPOINTER_TO_UINT (data);
sjoerd = create_contact (test, "sjoerd@example.com");
steve = create_contact (test, "steve@example.com");
/* Feature is not prepared yet */
g_object_get (test->connection, "blocked-contacts", &blocked, NULL);
g_assert_cmpuint (blocked->len, == , 0);
g_ptr_array_unref (blocked);
blocked = tp_connection_get_blocked_contacts (test->connection);
g_assert_cmpuint (blocked->len, == , 0);
/* Prepare the feature */
tp_proxy_prepare_async (test->connection, features,
proxy_prepare_cb, test);
test->wait = 1;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
/* 2 contacts are already blocked in the CM */
g_object_get (test->connection, "blocked-contacts", &blocked, NULL);
g_assert_cmpuint (blocked->len, == , 2);
g_ptr_array_unref (blocked);
blocked = tp_connection_get_blocked_contacts (test->connection);
g_assert_cmpuint (blocked->len, == , 2);
/* Preparing TP_CONNECTION_FEATURE_CONTACT_BLOCKING gives us
* TP_CONTACT_FEATURE_CONTACT_BLOCKING for free. Test that this works with
* existing and newly created TpContact. */
bill = create_contact (test, "bill@example.com");
guillaume = create_contact (test, "guillaume@example.com");
g_assert (tp_contact_has_feature (sjoerd,
TP_CONTACT_FEATURE_CONTACT_BLOCKING));
g_assert (!tp_contact_is_blocked (sjoerd));
g_assert (tp_contact_has_feature (steve,
TP_CONTACT_FEATURE_CONTACT_BLOCKING));
g_assert (tp_contact_is_blocked (steve));
g_assert (tp_contact_has_feature (bill, TP_CONTACT_FEATURE_CONTACT_BLOCKING));
g_assert (tp_contact_is_blocked (bill));
g_assert (tp_contact_has_feature (guillaume,
TP_CONTACT_FEATURE_CONTACT_BLOCKING));
g_assert (!tp_contact_is_blocked (guillaume));
g_object_unref (steve);
g_object_unref (sjoerd);
g_object_unref (bill);
g_object_unref (guillaume);
/* Let's block another contact */
alice = create_contact (test, "alice");
g_signal_connect (test->connection, "blocked-contacts-changed",
G_CALLBACK (blocked_contacts_changed_cb), test);
if (use_contact_api)
{
tp_contact_block_async (alice, FALSE, contact_block_cb, test);
}
else
{
tp_connection_block_contacts_async (test->connection,
1, &alice, FALSE, block_contacts_cb, test);
}
g_object_unref (alice);
test->wait = 2;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_assert_cmpuint (test->blocked_added->len, ==, 1);
g_assert_cmpuint (test->blocked_removed->len, ==, 0);
alice = g_ptr_array_index (test->blocked_added, 0);
g_assert (TP_IS_CONTACT (alice));
g_assert_cmpstr (tp_contact_get_identifier (alice), ==, "alice");
blocked = tp_connection_get_blocked_contacts (test->connection);
g_assert_cmpuint (blocked->len, == , 3);
/* Cool, now unblock the poor Alice */
if (use_contact_api)
{
tp_contact_unblock_async (alice, contact_unblock_cb, test);
}
else
{
tp_connection_unblock_contacts_async (test->connection,
1, &alice, unblock_contacts_cb, test);
}
test->wait = 2;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_assert_cmpuint (test->blocked_added->len, ==, 0);
g_assert_cmpuint (test->blocked_removed->len, ==, 1);
alice = g_ptr_array_index (test->blocked_removed, 0);
g_assert (TP_IS_CONTACT (alice));
g_assert_cmpstr (tp_contact_get_identifier (alice), ==, "alice");
blocked = tp_connection_get_blocked_contacts (test->connection);
g_assert_cmpuint (blocked->len, == , 2);
}
static void
dup_contact_by_id_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
TpConnection *connection = (TpConnection *) source;
Test *test = user_data;
g_clear_object (&test->contact);
test->contact = tp_connection_dup_contact_by_id_finish (connection, result,
&test->error);
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
contact_list_state_change_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
TpConnection *conn = (TpConnection *) object;
Test *test = user_data;
if (tp_connection_get_contact_list_state (conn) !=
TP_CONTACT_LIST_STATE_SUCCESS)
return;
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
property_change_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
Test *test = user_data;
test->wait--;
if (test->wait <= 0)
g_main_loop_quit (test->mainloop);
}
static void
test_is_blocked (Test *test,
gconstpointer data G_GNUC_UNUSED)
{
const gchar *id = "bill@example.com";
GQuark features[] = { TP_CONTACT_FEATURE_CONTACT_BLOCKING };
GQuark conn_features[] = { TP_CONNECTION_FEATURE_CONTACT_LIST, 0 };
tp_proxy_prepare_async (test->connection, conn_features,
proxy_prepare_cb, test);
test->wait = 1;
/* We have to wait that the ContactList has been fetched by the CM */
if (tp_connection_get_contact_list_state (test->connection) !=
TP_CONTACT_LIST_STATE_SUCCESS)
{
g_signal_connect (test->connection, "notify::contact-list-state",
G_CALLBACK (contact_list_state_change_cb), test);
test->wait++;
}
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
/* Bill is already blocked in the CM */
tp_connection_dup_contact_by_id_async (test->connection, id,
features, dup_contact_by_id_cb, test);
test->wait = 1;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_assert (TP_IS_CONTACT (test->contact));
g_assert (tp_contact_has_feature (test->contact,
TP_CONTACT_FEATURE_CONTACT_BLOCKING));
g_assert (tp_contact_is_blocked (test->contact));
/* Unblock Bill */
g_signal_connect (test->contact, "notify::is-blocked",
G_CALLBACK (property_change_cb), test);
tp_contact_unblock_async (test->contact, contact_unblock_cb, test);
test->wait = 2;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_assert (!tp_contact_is_blocked (test->contact));
}
static void
test_contact_list_properties (Test *test,
gconstpointer data G_GNUC_UNUSED)
{
gboolean props_only = GPOINTER_TO_UINT (data);
GQuark conn_features[] = { 0, 0 };
GPtrArray *contacts;
if (props_only)
conn_features[0] = TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES;
else
conn_features[0] = TP_CONNECTION_FEATURE_CONTACT_LIST;
/* Feature isn't prepared yet */
g_assert (!tp_proxy_is_prepared (test->connection,
TP_CONNECTION_FEATURE_CONTACT_LIST));
g_assert (!tp_proxy_is_prepared (test->connection,
TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES));
g_assert_cmpuint (tp_connection_get_contact_list_state (test->connection), ==,
TP_CONTACT_LIST_STATE_NONE);
g_assert (!tp_connection_get_contact_list_persists (test->connection));
g_assert (!tp_connection_get_can_change_contact_list (test->connection));
g_assert (!tp_connection_get_request_uses_message (test->connection));
tp_proxy_prepare_async (test->connection, conn_features,
proxy_prepare_cb, test);
test->wait = 1;
g_main_loop_run (test->mainloop);
g_assert_no_error (test->error);
g_assert (tp_proxy_is_prepared (test->connection,
TP_CONNECTION_FEATURE_CONTACT_LIST) == !props_only);
g_assert (tp_proxy_is_prepared (test->connection,
TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES));
g_assert (tp_connection_get_contact_list_persists (test->connection));
g_assert (tp_connection_get_can_change_contact_list (test->connection));
g_assert (tp_connection_get_request_uses_message (test->connection));
contacts = tp_connection_dup_contact_list (test->connection);
if (props_only)
{
/* Contacts haven't be fetched */
g_assert_cmpuint (contacts->len, ==, 0);
}
else
{
g_assert_cmpuint (contacts->len, >, 0);
}
g_ptr_array_unref (contacts);
}
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 ("/contact-list-client/blocking/block-unblock", Test, NULL, setup,
test_block_unblock, teardown);
g_test_add ("/contact-list-client/blocking/can-report-abusive", Test, NULL,
setup, test_can_report_abusive, teardown);
g_test_add ("/contact-list-client/blocking/connection/blocked-contacts", Test,
GUINT_TO_POINTER (FALSE), setup, test_blocked_contacts, teardown);
g_test_add ("/contact-list-client/blocking/contact/blocked-contacts", Test,
GUINT_TO_POINTER (TRUE), setup, test_blocked_contacts, teardown);
g_test_add ("/contact-list-client/blocking/is-blocked", Test, NULL,
setup, test_is_blocked, teardown);
g_test_add ("/contact-list-client/contact-list/properties", Test,
GUINT_TO_POINTER (FALSE), setup, test_contact_list_properties, teardown);
g_test_add ("/contact-list-client/contact-list/properties", Test,
GUINT_TO_POINTER (TRUE), setup, test_contact_list_properties, teardown);
return tp_tests_run_with_bus ();
}
|