vcard_tester.c 37.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	vcard_tester.c
	Copyright (C) 2015  Belledonne Communications SARL

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

19 20
#ifdef VCARD_ENABLED

21 22 23
#include "linphonecore.h"
#include "private.h"
#include "liblinphone_tester.h"
24
#include "carddav.h"
25

26 27
#include <time.h>

28 29
#define CARDDAV_SERVER "http://dav.linphone.org/card.php/addressbooks/tester/default"

30
static void linphone_vcard_import_export_friends_test(void) {
31
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
32
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
33
	const bctbx_list_t *friends = linphone_friend_list_get_friends(lfl);
34
	char *import_filepath = bc_tester_res("vcards/vcards.vcf");
35
	char *export_filepath = bc_tester_file("export_vcards.vcf");
36
	int count = 0;
37
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
38
	
39
	count = linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
40
	BC_ASSERT_EQUAL(count, 3, int, "%d");
41
	friends = linphone_friend_list_get_friends(lfl);
42
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 3, int, "%d");
43

44
	linphone_friend_list_export_friends_as_vcard4_file(lfl, export_filepath);
45

46 47
	lfl = linphone_core_create_friend_list(manager->lc);
	count = linphone_friend_list_import_friends_from_vcard4_file(lfl, export_filepath);
48
	BC_ASSERT_EQUAL(count, 3, int, "%d");
49
	friends = linphone_friend_list_get_friends(lfl);
50
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 3, int, "%d");
51
	linphone_friend_list_unref(lfl);
52

53
	remove(export_filepath);
54 55
	ms_free(import_filepath);
	ms_free(export_filepath);
56
	linphone_core_manager_destroy(manager);
57 58
}

59 60
static void linphone_vcard_import_a_lot_of_friends_test(void) {
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
61
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
62
	char *import_filepath = bc_tester_res("vcards/thousand_vcards.vcf");
63 64
	clock_t start, end;
	double elapsed = 0;
65
	const bctbx_list_t *friends = NULL;
66 67 68
	FILE    *infile = NULL;
	char    *buffer = NULL;
	long    numbytes = 0;
69 70

	start = clock();
71
	linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
72
	end = clock();
73

74
	friends = linphone_friend_list_get_friends(lfl);
75
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
76

77
	elapsed = (double)(end - start);
78
	ms_error("Imported a thousand of vCards from file (only %u friends with SIP address found) in %f seconds", (unsigned int)bctbx_list_size(friends), elapsed / CLOCKS_PER_SEC);
79

80
	lfl = linphone_core_create_friend_list(manager->lc);
81 82 83 84 85 86
	infile = fopen(import_filepath, "rb");
	BC_ASSERT_PTR_NOT_NULL(infile);
	if (infile) {
		fseek(infile, 0L, SEEK_END);
		numbytes = ftell(infile);
		fseek(infile, 0L, SEEK_SET);
87
		buffer = (char*)ms_malloc((numbytes + 1) * sizeof(char));
88 89
		numbytes = fread(buffer, sizeof(char), numbytes, infile);
		fclose(infile);
90
		buffer[numbytes] = '\0';
91

92 93 94 95 96
		start = clock();
		linphone_friend_list_import_friends_from_vcard4_buffer(lfl, buffer);
		end = clock();
		ms_free(buffer);
	}
97

98
	friends = linphone_friend_list_get_friends(lfl);
99
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
100

101
	elapsed = (double)(end - start);
102
	ms_error("Imported a thousand of vCards from buffer (only %u friends with SIP address found) in %f seconds", (unsigned int)bctbx_list_size(friends), elapsed / CLOCKS_PER_SEC);
103

104
	linphone_friend_list_unref(lfl);
105

106
	ms_free(import_filepath);
107 108 109
	linphone_core_manager_destroy(manager);
}

110 111 112 113 114 115 116 117 118
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif

119 120
static void linphone_vcard_update_existing_friends_test(void) {
	LinphoneFriend *lf = linphone_friend_new_with_addr("sip:oldfriend@sip.linphone.org");
121

122
	BC_ASSERT_PTR_NOT_NULL(lf);
123 124 125 126 127
	BC_ASSERT_PTR_NULL(linphone_friend_get_vcard(lf));

	linphone_friend_edit(lf);
	linphone_friend_set_name(lf, "Old Friend");
	linphone_friend_done(lf);
128

129 130
	BC_ASSERT_PTR_NOT_NULL(linphone_friend_get_vcard(lf));
	BC_ASSERT_STRING_EQUAL(linphone_vcard_get_full_name(linphone_friend_get_vcard(lf)), "Old Friend");
131 132
	linphone_friend_unref(lf);
	lf = NULL;
133 134
}

135 136 137 138
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif

139
static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
140
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
141
	LinphoneVcard *lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sberfini@sip.linphone.org\r\nIMPP;TYPE=home:sip:sylvain@sip.linphone.org\r\nTEL;TYPE=work:0952636505\r\nEND:VCARD\r\n");
142
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
143 144
	bctbx_list_t *sip_addresses = linphone_friend_get_addresses(lf);
	bctbx_list_t *phone_numbers = linphone_friend_get_phone_numbers(lf);
145
	LinphoneAddress *addr = NULL;
146

147 148 149 150
	BC_ASSERT_EQUAL(bctbx_list_size(sip_addresses), 2, int, "%i");
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 1, int, "%i");
	if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
	if (phone_numbers) bctbx_list_free(phone_numbers);
151
	linphone_friend_unref(lf);
152

153
	lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nTEL;TYPE=work:0952636505\r\nTEL:0476010203\r\nEND:VCARD\r\n");
154 155 156
	lf = linphone_friend_new_from_vcard(lvc);
	sip_addresses = linphone_friend_get_addresses(lf);
	phone_numbers = linphone_friend_get_phone_numbers(lf);
157

158 159 160 161
	BC_ASSERT_EQUAL(bctbx_list_size(sip_addresses), 0, int, "%i");
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 2, int, "%i");
	if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
	if (phone_numbers) bctbx_list_free(phone_numbers);
162

163 164 165 166
	addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	linphone_friend_add_address(lf, addr);
	linphone_address_unref(addr);
	sip_addresses = linphone_friend_get_addresses(lf);
167 168
	BC_ASSERT_EQUAL(bctbx_list_size(sip_addresses), 1, int, "%i");
	if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
169

170 171
	linphone_friend_remove_phone_number(lf, "0952636505");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
172 173
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 1, int, "%i");
	if (phone_numbers) bctbx_list_free(phone_numbers);
174

175 176
	linphone_friend_remove_phone_number(lf, "0476010203");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
177 178
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 0, int, "%i");
	if (phone_numbers) bctbx_list_free(phone_numbers);
179

180 181 182 183
	addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	linphone_friend_remove_address(lf, addr);
	linphone_address_unref(addr);
	sip_addresses = linphone_friend_get_addresses(lf);
184 185
	BC_ASSERT_EQUAL(bctbx_list_size(sip_addresses), 0, int, "%i");
	if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
186

187 188
	linphone_friend_add_phone_number(lf, "+33952636505");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
189 190
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 1, int, "%i");
	if (phone_numbers) bctbx_list_free(phone_numbers);
191

192 193 194
	linphone_friend_unref(lf);
	lf = NULL;
	lvc = NULL;
195
	linphone_core_manager_destroy(manager);
196 197
}

Ghislain MARY's avatar
Ghislain MARY committed
198
#ifdef SQLITE_STORAGE_ENABLED
199 200
static void friends_if_no_db_set(void) {
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
201
	LinphoneFriend *lf = linphone_core_create_friend(manager->lc);
202
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
203
	const bctbx_list_t *friends = NULL;
204
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
205

206 207
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
208 209
	linphone_friend_list_add_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(lfl);
210
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 1, int, "%d");
211

212 213 214
	linphone_friend_list_remove_friend(lfl, lf);
	linphone_friend_unref(lf);
	friends = linphone_friend_list_get_friends(lfl);
215
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
216

217
	linphone_friend_list_unref(lfl);
218
	linphone_address_unref(addr);
219 220 221 222
	linphone_core_manager_destroy(manager);
}

static void friends_migration(void) {
223
	LinphoneCoreManager* manager = linphone_core_manager_new2("friends_rc", FALSE);
224
	LpConfig *lpc = linphone_core_get_config(manager->lc);
225
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
226 227
	const bctbx_list_t *friends = linphone_friend_list_get_friends(lfl);
	bctbx_list_t *friends_from_db = NULL;
228
	char *friends_db = bc_tester_file("friends.db");
229
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 3, int, "%d");
230
	BC_ASSERT_EQUAL(lp_config_get_int(lpc, "misc", "friends_migration_done", 0), 0, int, "%i");
231

232 233
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
234
	lfl = linphone_core_get_default_friend_list(manager->lc);
235
	friends = linphone_friend_list_get_friends(lfl);
236
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 3, int, "%d");
237
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc, lfl);
238
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 3, int, "%d");
239
	BC_ASSERT_EQUAL(lp_config_get_int(lpc, "misc", "friends_migration_done", 0), 1, int, "%i");
240

241
	friends_from_db = bctbx_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
242 243 244 245 246
	unlink(friends_db);
	ms_free(friends_db);
	linphone_core_manager_destroy(manager);
}

247 248 249 250 251 252 253
typedef struct _LinphoneFriendListStats {
	int new_list_count;
	int removed_list_count;
} LinphoneFriendListStats;

static void friend_list_created_cb(LinphoneCore *lc, LinphoneFriendList *list) {
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)linphone_friend_list_get_user_data(list);
254 255 256
	if (stats) {
		stats->new_list_count++;
	}
257 258 259 260
}

static void friend_list_removed_cb(LinphoneCore *lc, LinphoneFriendList *list) {
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)linphone_friend_list_get_user_data(list);
261 262 263
	if (stats) {
		stats->removed_list_count++;
	}
264 265
}

266
static void friends_sqlite_storage(void) {
267 268 269
	LinphoneCoreVTable *v_table = linphone_core_v_table_new();
	LinphoneCore* lc = NULL;
	LinphoneFriendList *lfl = NULL;
270
	LinphoneFriend *lf = NULL;
271
	LinphoneFriend *lf2 = NULL;
272
	LinphoneVcard *lvc = linphone_vcard_new();
273
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
274 275 276
	const bctbx_list_t *friends = NULL;
	bctbx_list_t *friends_from_db = NULL;
	bctbx_list_t *friends_lists_from_db = NULL;
277
	char *friends_db = bc_tester_file("friends.db");
278
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)ms_new0(LinphoneFriendListStats, 1);
279
	char *address = NULL, *address2 = NULL;
280

281 282 283
	v_table->friend_list_created = friend_list_created_cb;
	v_table->friend_list_removed = friend_list_removed_cb;
	lc = linphone_core_new(v_table, NULL, NULL, NULL);
284
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
285 286
	lfl = linphone_core_create_friend_list(lc);
	linphone_friend_list_set_user_data(lfl, stats);
287
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
288

289
	unlink(friends_db);
290 291
	linphone_core_set_friends_database_path(lc, friends_db);
	friends_from_db = linphone_core_fetch_friends_from_db(lc, linphone_core_get_default_friend_list(lc));
292
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 0, int, "%d");
293

294 295
	linphone_vcard_set_etag(lvc, "\"123-456789\"");
	linphone_vcard_set_url(lvc, "http://dav.somewhere.fr/addressbook/me/someone.vcf");
296
	lf = linphone_friend_new_from_vcard(lvc);
297 298
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
299

300 301 302
	linphone_core_add_friend_list(lc, lfl);
	wait_for_until(lc, NULL, &stats->new_list_count, 1, 1000);
	BC_ASSERT_EQUAL(stats->new_list_count, 1, int, "%i");
303
	linphone_friend_list_unref(lfl);
304
	linphone_friend_list_set_display_name(lfl, "Test");
305
	BC_ASSERT_EQUAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%i");
306
	linphone_friend_unref(lf);
307 308
	BC_ASSERT_EQUAL(lfl->storage_id, 1, unsigned int, "%u");
	BC_ASSERT_EQUAL(lf->storage_id, 1, unsigned int, "%u");
309

310
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
311
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
312

313
	friends_lists_from_db = linphone_core_fetch_friends_lists_from_db(lc);
314
	BC_ASSERT_EQUAL(bctbx_list_size(friends_lists_from_db), 1, int, "%d");
315
	friends_from_db = ((LinphoneFriendList *)friends_lists_from_db->data)->friends;
316
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 1, int, "%d");
317 318 319
	lf2 = (LinphoneFriend *)friends_from_db->data;
	BC_ASSERT_PTR_NOT_NULL(lf2->lc);
	BC_ASSERT_PTR_NOT_NULL(lf2->friend_list);
320
	friends_lists_from_db = bctbx_list_free_with_data(friends_lists_from_db, (void (*)(void *))linphone_friend_list_unref);
321

322
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
323 324
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 1, int, "%d");
	if (bctbx_list_size(friends_from_db) < 1) {
325 326 327 328
		goto end;
	}
	lf2 = (LinphoneFriend *)friends_from_db->data;
	BC_ASSERT_STRING_EQUAL(linphone_friend_get_name(lf2), linphone_friend_get_name(lf));
329
	BC_ASSERT_EQUAL(lf2->storage_id, lf->storage_id, unsigned int, "%u");
330 331
	BC_ASSERT_STRING_EQUAL(linphone_vcard_get_etag(linphone_friend_get_vcard(lf2)), linphone_vcard_get_etag(linphone_friend_get_vcard(lf)));
	BC_ASSERT_STRING_EQUAL(linphone_vcard_get_url(linphone_friend_get_vcard(lf2)), linphone_vcard_get_url(linphone_friend_get_vcard(lf)));
332 333 334 335 336
	address = linphone_address_as_string(linphone_friend_get_address(lf));
	address2 = linphone_address_as_string(linphone_friend_get_address(lf2));
	BC_ASSERT_STRING_EQUAL(address2, address);
	ms_free(address);
	ms_free(address2);
337 338 339 340

	linphone_friend_edit(lf);
	linphone_friend_set_name(lf, "Margaux");
	linphone_friend_done(lf);
341
	friends_from_db = bctbx_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
342
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
343 344
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 1, int, "%d");
	if (bctbx_list_size(friends_from_db) < 1) {
345 346
		goto end;
	}
347
	lf2 = (LinphoneFriend *)friends_from_db->data;
348
	BC_ASSERT_STRING_EQUAL(linphone_friend_get_name(lf2), "Margaux");
349
	friends_from_db = bctbx_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
350

351 352
	linphone_friend_list_remove_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
353
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
354
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
355
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 0, int, "%d");
356

357 358 359
	linphone_core_remove_friend_list(lc, lfl);
	wait_for_until(lc, NULL, &stats->removed_list_count, 1, 1000);
	BC_ASSERT_EQUAL(stats->removed_list_count, 1, int, "%i");
360 361

end:
362
	ms_free(stats);
363 364
	unlink(friends_db);
	ms_free(friends_db);
365
	linphone_address_unref(addr);
366
	linphone_core_destroy(lc);
367
	linphone_core_v_table_destroy(v_table);
368 369
}
#endif
370

371 372 373 374 375 376 377
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

378
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
379
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
380
	BC_ASSERT_TRUE(success);
Sylvain Berfini's avatar
Sylvain Berfini committed
381
	stats->sync_done_count++;
382 383
}

384 385
static void carddav_new_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
386
	BC_ASSERT_PTR_NOT_NULL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
387
	stats->new_contact_count++;
388 389 390 391
}

static void carddav_removed_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
392
	BC_ASSERT_PTR_NOT_NULL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
393
	stats->removed_contact_count++;
394 395
}

396
static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *new_lf, LinphoneFriend *old_lf) {
397
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
398 399
	BC_ASSERT_PTR_NOT_NULL(new_lf);
	BC_ASSERT_PTR_NOT_NULL(old_lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
400
	stats->updated_contact_count++;
401 402
}

403
static void carddav_sync(void) {
404
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
405
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
406 407
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
408

409
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
410
	linphone_core_add_friend_list(manager->lc, lfl);
411
	linphone_friend_list_unref(lfl);
412
	c = linphone_carddav_context_new(lfl);
413
	BC_ASSERT_PTR_NOT_NULL(c);
414

415
	linphone_carddav_set_user_data(c, stats);
416
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
417 418 419
	linphone_carddav_set_new_contact_callback(c, carddav_new_contact);
	linphone_carddav_set_removed_contact_callback(c, carddav_removed_contact);
	linphone_carddav_set_updated_contact_callback(c, carddav_updated_contact);
420
	linphone_carddav_synchronize(c);
421

422
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
423
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
424
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
425
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
426

Sylvain Berfini's avatar
Sylvain Berfini committed
427
	ms_free(stats);
428
	linphone_carddav_context_destroy(c);
429 430
	linphone_core_manager_destroy(manager);
}
Sylvain Berfini's avatar
Sylvain Berfini committed
431 432

static void carddav_sync_2(void) {
433
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
434
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
435
	LinphoneFriend *lf = linphone_core_create_friend_with_address(manager->lc, "\"Sylvain\" <sip:sylvain@sip.linphone.org>");
436
	char *friends_db = bc_tester_file("friends.db");
437 438
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
439

440
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
441
	linphone_core_add_friend_list(manager->lc, lfl);
442
	linphone_friend_list_unref(lfl);
443
	c = linphone_carddav_context_new(lfl);
444
	BC_ASSERT_PTR_NOT_NULL(c);
445

Sylvain Berfini's avatar
Sylvain Berfini committed
446 447
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
448
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
449
	linphone_friend_unref(lf);
450

Sylvain Berfini's avatar
Sylvain Berfini committed
451 452 453 454 455
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
	linphone_carddav_set_new_contact_callback(c, carddav_new_contact);
	linphone_carddav_set_removed_contact_callback(c, carddav_removed_contact);
	linphone_carddav_set_updated_contact_callback(c, carddav_updated_contact);
456

Sylvain Berfini's avatar
Sylvain Berfini committed
457
	linphone_carddav_synchronize(c);
458

459
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
460
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
461
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
462
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
463
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
464
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
465

Sylvain Berfini's avatar
Sylvain Berfini committed
466 467 468
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
469
	linphone_carddav_context_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
470 471 472 473
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
474
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
475
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
476
	LinphoneVcard *lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:1f08dd48-29ac-4097-8e48-8596d7776283\r\nFN:Sylvain Berfini\r\nIMPP;TYPE=work:sip:sylvain@sip.linphone.org\r\nEND:VCARD\r\n");
Sylvain Berfini's avatar
Sylvain Berfini committed
477
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
478
	char *friends_db = bc_tester_file("friends.db");
479 480
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
481

482
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
483
	linphone_core_add_friend_list(manager->lc, lfl);
484
	linphone_friend_list_unref(lfl);
485
	c = linphone_carddav_context_new(lfl);
486
	BC_ASSERT_PTR_NOT_NULL(c);
487

Sylvain Berfini's avatar
Sylvain Berfini committed
488 489
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
490
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
491
	linphone_friend_unref(lf);
492

Sylvain Berfini's avatar
Sylvain Berfini committed
493 494 495 496 497
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
	linphone_carddav_set_new_contact_callback(c, carddav_new_contact);
	linphone_carddav_set_removed_contact_callback(c, carddav_removed_contact);
	linphone_carddav_set_updated_contact_callback(c, carddav_updated_contact);
498

Sylvain Berfini's avatar
Sylvain Berfini committed
499
	linphone_carddav_synchronize(c);
500

501
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
502
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
503
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
504
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
505 506 507 508

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
509
	linphone_carddav_context_destroy(c);
510
	c = NULL;
511 512 513 514
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_4(void) {
515
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
516
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
517
	LinphoneVcard *lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Margaux Clerc\r\nIMPP;TYPE=work:sip:margaux@sip.linphone.org\r\nEND:VCARD\r\n");
518
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
519 520
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
521

522
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
523
	linphone_core_add_friend_list(manager->lc, lfl);
524
	linphone_friend_list_unref(lfl);
525
	c = linphone_carddav_context_new(lfl);
526
	BC_ASSERT_PTR_NOT_NULL(c);
527

528 529
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
530 531 532
	linphone_carddav_set_new_contact_callback(c, carddav_new_contact);
	linphone_carddav_set_removed_contact_callback(c, carddav_removed_contact);
	linphone_carddav_set_updated_contact_callback(c, carddav_updated_contact);
533 534

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
535
	linphone_carddav_put_vcard(c, lf);
536
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
537
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
538
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
539

540
	linphone_carddav_delete_vcard(c, lf);
541
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
542 543 544 545
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");

	linphone_friend_unref(lf);
	ms_free(stats);
546 547 548 549 550 551 552 553 554 555 556 557 558 559
	linphone_carddav_context_destroy(c);
	linphone_core_manager_destroy(manager);
}

static void carddav_contact_created(LinphoneFriendList *list, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_friend_list_cbs_get_user_data(list->cbs);
	stats->new_contact_count++;
}

static void carddav_contact_deleted(LinphoneFriendList *list, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_friend_list_cbs_get_user_data(list->cbs);
	stats->removed_contact_count++;
}

560 561
static void carddav_contact_updated(LinphoneFriendList *list, LinphoneFriend *new_friend, LinphoneFriend *old_friend) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_friend_list_cbs_get_user_data(list->cbs);
562
	BC_ASSERT_STRING_EQUAL(linphone_vcard_get_full_name(linphone_friend_get_vcard(new_friend)), linphone_vcard_get_full_name(linphone_friend_get_vcard(old_friend)));
563 564 565
	stats->updated_contact_count++;
}

566 567 568 569 570 571 572 573 574
static void carddav_sync_status_changed(LinphoneFriendList *list, LinphoneFriendListSyncStatus status, const char *msg) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_friend_list_cbs_get_user_data(list->cbs);
	char *state = status == LinphoneFriendListSyncStarted ? "Sync started" : (status == LinphoneFriendListSyncFailure ? "Sync failure" : "Sync successful");
	ms_message("[CardDAV] %s : %s", state, msg);
	if (status == LinphoneFriendListSyncFailure || status == LinphoneFriendListSyncSuccessful) {
		stats->sync_done_count++;
	}
}

575
static void carddav_integration(void) {
576
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
577
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
578
	LinphoneVcard *lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Margaux Clerc\r\nIMPP;TYPE=work:sip:margaux@sip.linphone.org\r\nEND:VCARD\r\n");
579
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
580
	LinphoneVcard *lvc2 = NULL;
581
	LinphoneFriend *lf2 = NULL;
582 583
	LinphoneFriendListCbs *cbs = NULL;
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
584
	const char *refkey = "toto";
585
	char *address = NULL;
586

587
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
588 589 590 591
	cbs = linphone_friend_list_get_callbacks(lfl);
	linphone_friend_list_cbs_set_user_data(cbs, stats);
	linphone_friend_list_cbs_set_contact_created(cbs, carddav_contact_created);
	linphone_friend_list_cbs_set_contact_deleted(cbs, carddav_contact_deleted);
592
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
593
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
594
	linphone_core_add_friend_list(manager->lc, lfl);
595 596

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
597
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
598
	BC_ASSERT_EQUAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
599
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->dirty_friends_to_update), 1, int, "%d");
600
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
601
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
602
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
603
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
604
	linphone_friend_list_remove_friend(lfl, lf);
605
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->friends), 0, int, "%d");
606
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
607
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
608
	linphone_friend_unref(lf);
609
	lf = NULL;
610

611
	lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Ghislain Mary\r\nIMPP;TYPE=work:sip:ghislain@sip.linphone.org\r\nEND:VCARD\r\n");
612
	lf = linphone_friend_new_from_vcard(lvc);
613
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
614
	linphone_friend_unref(lf);
615

616
	lvc2 = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sberfini@sip.linphone.org\r\nUID:1f08dd48-29ac-4097-8e48-8596d7776283\r\nEND:VCARD\r\n");
617
	linphone_vcard_set_url(lvc2, "/card.php/addressbooks/tester/default/me.vcf");
618
	lf2 = linphone_friend_new_from_vcard(lvc2);
619
	linphone_friend_set_ref_key(lf2, refkey);
620
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf2), LinphoneFriendListOK, int, "%d");
621

622 623
	BC_ASSERT_EQUAL(lfl->revision, 0, int, "%i");
	linphone_friend_list_synchronize_friends_from_server(lfl);
624
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 0, 5000);
625
	BC_ASSERT_EQUAL(stats->new_contact_count, 0, int, "%i");
626
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 5000);
627
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
628
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 5000);
629
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
630
	BC_ASSERT_NOT_EQUAL(lfl->revision, 0, int, "%i");
631
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 5000);
632
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
633

634
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->friends), 1, int, "%i");
635
	lf = (LinphoneFriend *)lfl->friends->data;
636
	BC_ASSERT_STRING_EQUAL(lf->refkey, refkey);
637
	BC_ASSERT_EQUAL(lf->storage_id, lf2->storage_id, unsigned int, "%u");
638
	linphone_friend_unref(lf2);
639 640 641
	address = linphone_address_as_string_uri_only(lf->uri);
	BC_ASSERT_STRING_EQUAL(address, "sip:sylvain@sip.linphone.org");
	ms_free(address);
642

643 644
	linphone_friend_edit(lf);
	linphone_friend_done(lf);
645
	BC_ASSERT_EQUAL(bctbx_list_size(lf->friend_list->dirty_friends_to_update), 0, int, "%i");
646

647 648 649 650
	linphone_core_set_network_reachable(manager->lc, FALSE); //To prevent the CardDAV update
	linphone_friend_edit(lf);
	linphone_friend_set_name(lf, "François Grisez");
	linphone_friend_done(lf);
651
	BC_ASSERT_EQUAL(bctbx_list_size(lf->friend_list->dirty_friends_to_update), 1, int, "%i");
652

653 654
	ms_free(stats);
	linphone_friend_list_unref(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
655 656 657
	linphone_core_manager_destroy(manager);
}

658 659 660 661 662
static void carddav_clean(void) {  // This is to ensure the content of the test addressbook is in the correct state for the following tests
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(lfl);
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
663 664
	bctbx_list_t *friends = NULL;
	bctbx_list_t *friends_iterator = NULL;
665
	LinphoneFriend *lf = NULL;
666
	LinphoneVcard *lvc = NULL;
667

668 669 670 671
	linphone_friend_list_cbs_set_user_data(cbs, stats);
	linphone_friend_list_cbs_set_contact_created(cbs, carddav_contact_created);
	linphone_friend_list_cbs_set_contact_deleted(cbs, carddav_contact_deleted);
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
672
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
673
	linphone_core_add_friend_list(manager->lc, lfl);
674
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
675

676
	linphone_friend_list_synchronize_friends_from_server(lfl);
677
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
678 679
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
	stats->sync_done_count = 0;
680

681
	friends = bctbx_list_copy(lfl->friends);
682 683 684
	friends_iterator = friends;
	while (friends_iterator) {
		LinphoneFriend *lf = (LinphoneFriend *)friends_iterator->data;
685
		linphone_friend_list_remove_friend(lfl, lf);
686
		wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
687 688
		BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
		stats->sync_done_count = 0;
689
		stats->removed_contact_count = 0;
690
		friends_iterator = bctbx_list_next(friends_iterator);
691
	}
692
	bctbx_list_free(friends);
693

694
	lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sylvain@sip.linphone.org\r\nUID:1f08dd48-29ac-4097-8e48-8596d7776283\r\nEND:VCARD\r\n");
695
	linphone_vcard_set_url(lvc, "http://dav.linphone.org/card.php/addressbooks/tester/default/me.vcf");
696 697
	lf = linphone_friend_new_from_vcard(lvc);
	linphone_friend_list_add_friend(lfl, lf);
698
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
699
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
700

701 702 703 704 705 706
	ms_free(stats);
	linphone_friend_unref(lf);
	linphone_friend_list_unref(lfl);
	linphone_core_manager_destroy(manager);
}

707 708 709 710 711
static void carddav_multiple_sync(void) {
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(lfl);
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
712

713 714 715 716
	linphone_friend_list_cbs_set_user_data(cbs, stats);
	linphone_friend_list_cbs_set_contact_created(cbs, carddav_contact_created);
	linphone_friend_list_cbs_set_contact_deleted(cbs, carddav_contact_deleted);
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
717
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
718
	linphone_core_add_friend_list(manager->lc, lfl);
719
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
720

721
	linphone_friend_list_synchronize_friends_from_server(lfl);
722
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
723
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
724
	linphone_friend_list_synchronize_friends_from_server(lfl);
725
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
726
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
727
	linphone_friend_list_synchronize_friends_from_server(lfl);
728
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 5000);
729
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
730
	BC_ASSERT_EQUAL(stats->removed_contact_count, 0, int, "%i");
731

732
	ms_free(stats);
733 734 735 736
	linphone_friend_list_unref(lfl);
	linphone_core_manager_destroy(manager);
}

737 738 739 740 741
static void carddav_server_to_client_and_client_to_sever_sync(void) {
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(lfl);
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
742
	LinphoneVcard *lvc1 = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Margaux Clerc\r\nIMPP;TYPE=work:sip:margaux@sip.linphone.org\r\nEND:VCARD\r\n");
743
	LinphoneFriend *lf1 = linphone_friend_new_from_vcard(lvc1);
744
	LinphoneVcard *lvc2 = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Ghislain Mary\r\nIMPP;TYPE=work:sip:ghislain@sip.linphone.org\r\nEND:VCARD\r\n");
745
	LinphoneFriend *lf2 = linphone_friend_new_from_vcard(lvc2);
746
	bctbx_list_t *friends = NULL, *friends_iterator = NULL;
747

748 749 750 751 752 753 754
	linphone_friend_list_cbs_set_user_data(cbs, stats);
	linphone_friend_list_cbs_set_contact_created(cbs, carddav_contact_created);
	linphone_friend_list_cbs_set_contact_deleted(cbs, carddav_contact_deleted);
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
	linphone_core_add_friend_list(manager->lc, lfl);
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
755

756
	linphone_friend_list_add_friend(lfl, lf1);
757
	linphone_friend_unref(lf1);
758 759
	linphone_friend_list_synchronize_friends_from_server(lfl);
	linphone_friend_list_add_friend(lfl, lf2);
760
	linphone_friend_unref(lf2);
761 762
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 15000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
763

764
	stats->sync_done_count = 0;
765
	friends = bctbx_list_copy(lfl->friends);
766 767 768 769 770 771 772 773 774
	friends_iterator = friends;
	while (friends_iterator) {
		LinphoneFriend *lf = (LinphoneFriend *)friends_iterator->data;
		if (lf && strcmp(linphone_friend_get_name(lf), "Sylvain Berfini") != 0) {
			linphone_friend_list_remove_friend(lfl, lf);
			wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
			BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
			stats->sync_done_count = 0;
		}
775
		friends_iterator = bctbx_list_next(friends_iterator);
776
	}
777
	bctbx_list_free(friends);
778 779

	ms_free(stats);
780
	linphone_friend_list_unref(lfl);
781 782 783
	linphone_core_manager_destroy(manager);
}

784
test_t vcard_tests[] = {
785 786 787 788
	TEST_NO_TAG("Import / Export friends from vCards", linphone_vcard_import_export_friends_test),
	TEST_NO_TAG("Import a lot of friends from vCards", linphone_vcard_import_a_lot_of_friends_test),
	TEST_NO_TAG("vCard creation for existing friends", linphone_vcard_update_existing_friends_test),
	TEST_NO_TAG("vCard phone numbers and SIP addresses", linphone_vcard_phone_numbers_and_sip_addresses),
Ghislain MARY's avatar
Ghislain MARY committed
789
#ifdef SQLITE_STORAGE_ENABLED
790 791 792
	TEST_ONE_TAG("Friends working if no db set", friends_if_no_db_set, "LeaksMemory"),
	TEST_ONE_TAG("Friends storage migration from rc to db", friends_migration, "LeaksMemory"),
	TEST_NO_TAG("Friends storage in sqlite database", friends_sqlite_storage),
793
#endif
794 795 796 797 798 799 800 801
	TEST_NO_TAG("CardDAV clean", carddav_clean), // This is to ensure the content of the test addressbook is in the correct state for the following tests
	TEST_NO_TAG("CardDAV synchronization", carddav_sync),
	TEST_NO_TAG("CardDAV synchronization 2", carddav_sync_2),
	TEST_NO_TAG("CardDAV synchronization 3", carddav_sync_3),
	TEST_NO_TAG("CardDAV synchronization 4", carddav_sync_4),
	TEST_NO_TAG("CardDAV integration", carddav_integration),
	TEST_NO_TAG("CardDAV multiple synchronizations", carddav_multiple_sync),
	TEST_NO_TAG("CardDAV client to server and server to client sync", carddav_server_to_client_and_client_to_sever_sync)
802 803 804
};

test_suite_t vcard_test_suite = {
805
	"VCard", NULL, NULL,
806 807 808
	liblinphone_tester_before_each, liblinphone_tester_after_each,
	sizeof(vcard_tests) / sizeof(vcard_tests[0]), vcard_tests
};
809 810

#endif