vcard_tester.c 37.8 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"

Sylvain Berfini's avatar
Sylvain Berfini committed
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);
Sylvain Berfini's avatar
Sylvain Berfini committed
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

Sylvain Berfini's avatar
Sylvain Berfini committed
53
	remove(export_filepath);
54 55
	ms_free(import_filepath);
	ms_free(export_filepath);
Sylvain Berfini's avatar
Sylvain Berfini committed
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);
Ghislain MARY's avatar
Ghislain MARY committed
123 124 125 126 127
	if (linphone_core_vcard_supported()) {
		BC_ASSERT_PTR_NOT_NULL(linphone_friend_get_vcard(lf));
	} else {
		BC_ASSERT_PTR_NULL(linphone_friend_get_vcard(lf));
	}
128 129 130 131

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

133 134
	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");
Sylvain Berfini's avatar
Sylvain Berfini committed
135 136
	linphone_friend_unref(lf);
	lf = NULL;
137 138
}

139 140 141 142
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif

Sylvain Berfini's avatar
Sylvain Berfini committed
143
static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
144
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
145
	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");
Sylvain Berfini's avatar
Sylvain Berfini committed
146
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
147 148
	bctbx_list_t *sip_addresses = linphone_friend_get_addresses(lf);
	bctbx_list_t *phone_numbers = linphone_friend_get_phone_numbers(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
149
	LinphoneAddress *addr = NULL;
150

151 152 153 154
	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);
Sylvain Berfini's avatar
Sylvain Berfini committed
155
	linphone_friend_unref(lf);
156

157
	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");
Sylvain Berfini's avatar
Sylvain Berfini committed
158 159 160
	lf = linphone_friend_new_from_vcard(lvc);
	sip_addresses = linphone_friend_get_addresses(lf);
	phone_numbers = linphone_friend_get_phone_numbers(lf);
161

162 163 164 165
	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);
166

Sylvain Berfini's avatar
Sylvain Berfini committed
167 168 169 170
	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);
171 172
	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);
173

Sylvain Berfini's avatar
Sylvain Berfini committed
174 175
	linphone_friend_remove_phone_number(lf, "0952636505");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
176 177
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 1, int, "%i");
	if (phone_numbers) bctbx_list_free(phone_numbers);
178

Sylvain Berfini's avatar
Sylvain Berfini committed
179 180
	linphone_friend_remove_phone_number(lf, "0476010203");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
181 182
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 0, int, "%i");
	if (phone_numbers) bctbx_list_free(phone_numbers);
183

Sylvain Berfini's avatar
Sylvain Berfini committed
184 185 186 187
	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);
188 189
	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);
190

Sylvain Berfini's avatar
Sylvain Berfini committed
191 192
	linphone_friend_add_phone_number(lf, "+33952636505");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
193 194
	BC_ASSERT_EQUAL(bctbx_list_size(phone_numbers), 1, int, "%i");
	if (phone_numbers) bctbx_list_free(phone_numbers);
195

Sylvain Berfini's avatar
Sylvain Berfini committed
196 197 198
	linphone_friend_unref(lf);
	lf = NULL;
	lvc = NULL;
199
	linphone_core_manager_destroy(manager);
Sylvain Berfini's avatar
Sylvain Berfini committed
200 201
}

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

210 211
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
212 213
	linphone_friend_list_add_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(lfl);
214
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 1, int, "%d");
215

216 217 218
	linphone_friend_list_remove_friend(lfl, lf);
	linphone_friend_unref(lf);
	friends = linphone_friend_list_get_friends(lfl);
219
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
220

221
	linphone_friend_list_unref(lfl);
222
	linphone_address_unref(addr);
223 224 225 226
	linphone_core_manager_destroy(manager);
}

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

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

245
	friends_from_db = bctbx_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
246 247 248 249 250
	unlink(friends_db);
	ms_free(friends_db);
	linphone_core_manager_destroy(manager);
}

251 252 253 254 255 256 257
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);
258 259 260
	if (stats) {
		stats->new_list_count++;
	}
261 262 263 264
}

static void friend_list_removed_cb(LinphoneCore *lc, LinphoneFriendList *list) {
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)linphone_friend_list_get_user_data(list);
265 266 267
	if (stats) {
		stats->removed_list_count++;
	}
268 269
}

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

286 287 288
	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);
289
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
290 291
	lfl = linphone_core_create_friend_list(lc);
	linphone_friend_list_set_user_data(lfl, stats);
292
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
293

294
	unlink(friends_db);
295 296
	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));
297
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 0, int, "%d");
298

299 300
	linphone_vcard_set_etag(lvc, "\"123-456789\"");
	linphone_vcard_set_url(lvc, "http://dav.somewhere.fr/addressbook/me/someone.vcf");
301
	lf = linphone_friend_new_from_vcard(lvc);
302 303
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
304

305 306 307
	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");
308
	linphone_friend_list_unref(lfl);
309
	linphone_friend_list_set_display_name(lfl, "Test");
310
	BC_ASSERT_EQUAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%i");
311
	linphone_friend_unref(lf);
312 313
	BC_ASSERT_EQUAL(lfl->storage_id, 1, unsigned int, "%u");
	BC_ASSERT_EQUAL(lf->storage_id, 1, unsigned int, "%u");
314

315
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
316
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
317

318
	friends_lists_from_db = linphone_core_fetch_friends_lists_from_db(lc);
319
	BC_ASSERT_EQUAL(bctbx_list_size(friends_lists_from_db), 1, int, "%d");
320
	friends_from_db = ((LinphoneFriendList *)friends_lists_from_db->data)->friends;
321
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 1, int, "%d");
322 323 324
	lf2 = (LinphoneFriend *)friends_from_db->data;
	BC_ASSERT_PTR_NOT_NULL(lf2->lc);
	BC_ASSERT_PTR_NOT_NULL(lf2->friend_list);
325
	friends_lists_from_db = bctbx_list_free_with_data(friends_lists_from_db, (void (*)(void *))linphone_friend_list_unref);
326

327
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
328 329
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 1, int, "%d");
	if (bctbx_list_size(friends_from_db) < 1) {
330 331 332 333
		goto end;
	}
	lf2 = (LinphoneFriend *)friends_from_db->data;
	BC_ASSERT_STRING_EQUAL(linphone_friend_get_name(lf2), linphone_friend_get_name(lf));
334
	BC_ASSERT_EQUAL(lf2->storage_id, lf->storage_id, unsigned int, "%u");
335 336
	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)));
337 338 339 340
	laddress = linphone_friend_get_address(lf);
	address = linphone_address_as_string(laddress);
	laddress2 = linphone_friend_get_address(lf2);
	address2 = linphone_address_as_string(laddress2);
341
	BC_ASSERT_STRING_EQUAL(address2, address);
342 343
	linphone_address_unref(laddress);
	linphone_address_unref(laddress2);
344 345
	ms_free(address);
	ms_free(address2);
346 347 348 349

	linphone_friend_edit(lf);
	linphone_friend_set_name(lf, "Margaux");
	linphone_friend_done(lf);
350
	friends_from_db = bctbx_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
351
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
352 353
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 1, int, "%d");
	if (bctbx_list_size(friends_from_db) < 1) {
354 355
		goto end;
	}
356
	lf2 = (LinphoneFriend *)friends_from_db->data;
357
	BC_ASSERT_STRING_EQUAL(linphone_friend_get_name(lf2), "Margaux");
358
	friends_from_db = bctbx_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
359

360 361
	linphone_friend_list_remove_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
362
	BC_ASSERT_EQUAL(bctbx_list_size(friends), 0, int, "%d");
363
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
364
	BC_ASSERT_EQUAL(bctbx_list_size(friends_from_db), 0, int, "%d");
365

366 367 368
	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");
369 370

end:
371
	ms_free(stats);
372 373
	unlink(friends_db);
	ms_free(friends_db);
374
	linphone_address_unref(addr);
375
	linphone_core_destroy(lc);
376
	linphone_core_v_table_destroy(v_table);
377 378
}
#endif
379

380 381 382 383 384 385 386
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

387
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
388
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
389
	BC_ASSERT_TRUE(success);
Sylvain Berfini's avatar
Sylvain Berfini committed
390
	stats->sync_done_count++;
391 392
}

393 394
static void carddav_new_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
395
	BC_ASSERT_PTR_NOT_NULL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
396
	stats->new_contact_count++;
397 398 399 400
}

static void carddav_removed_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
401
	BC_ASSERT_PTR_NOT_NULL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
402
	stats->removed_contact_count++;
403 404
}

405
static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *new_lf, LinphoneFriend *old_lf) {
406
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
407 408
	BC_ASSERT_PTR_NOT_NULL(new_lf);
	BC_ASSERT_PTR_NOT_NULL(old_lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
409
	stats->updated_contact_count++;
410 411
}

412
static void carddav_sync(void) {
413
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
414
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
415 416
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
417

418
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
419
	linphone_core_add_friend_list(manager->lc, lfl);
420
	linphone_friend_list_unref(lfl);
421
	c = linphone_carddav_context_new(lfl);
422
	BC_ASSERT_PTR_NOT_NULL(c);
423

424
	linphone_carddav_set_user_data(c, stats);
425
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
426 427 428
	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);
429
	linphone_carddav_synchronize(c);
430

431
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
432
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
433
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
434
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
435

Sylvain Berfini's avatar
Sylvain Berfini committed
436
	ms_free(stats);
437
	linphone_carddav_context_destroy(c);
438 439
	linphone_core_manager_destroy(manager);
}
Sylvain Berfini's avatar
Sylvain Berfini committed
440 441

static void carddav_sync_2(void) {
442
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
443
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
444
	LinphoneFriend *lf = linphone_core_create_friend_with_address(manager->lc, "\"Sylvain\" <sip:sylvain@sip.linphone.org>");
445
	char *friends_db = bc_tester_file("friends.db");
446 447
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
448

449
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
450
	linphone_core_add_friend_list(manager->lc, lfl);
451
	linphone_friend_list_unref(lfl);
452
	c = linphone_carddav_context_new(lfl);
453
	BC_ASSERT_PTR_NOT_NULL(c);
454

Sylvain Berfini's avatar
Sylvain Berfini committed
455 456
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
457
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
458
	linphone_friend_unref(lf);
459

Sylvain Berfini's avatar
Sylvain Berfini committed
460 461 462 463 464
	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);
465

Sylvain Berfini's avatar
Sylvain Berfini committed
466
	linphone_carddav_synchronize(c);
467

468
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
469
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
470
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
471
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
472
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
473
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
474

Sylvain Berfini's avatar
Sylvain Berfini committed
475 476 477
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
478
	linphone_carddav_context_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
479 480 481 482
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
483
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
484
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
485
	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
486
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
487
	char *friends_db = bc_tester_file("friends.db");
488 489
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
490

491
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
492
	linphone_core_add_friend_list(manager->lc, lfl);
493
	linphone_friend_list_unref(lfl);
494
	c = linphone_carddav_context_new(lfl);
495
	BC_ASSERT_PTR_NOT_NULL(c);
496

Sylvain Berfini's avatar
Sylvain Berfini committed
497 498
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
499
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
500
	linphone_friend_unref(lf);
501

Sylvain Berfini's avatar
Sylvain Berfini committed
502 503 504 505 506
	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);
507

Sylvain Berfini's avatar
Sylvain Berfini committed
508
	linphone_carddav_synchronize(c);
509

510
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
511
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
512
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
513
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
514 515 516 517

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
518
	linphone_carddav_context_destroy(c);
519
	c = NULL;
520 521 522 523
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_4(void) {
524
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
525
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
526
	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");
527
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
528 529
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
530

531
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
532
	linphone_core_add_friend_list(manager->lc, lfl);
533
	linphone_friend_list_unref(lfl);
534
	c = linphone_carddav_context_new(lfl);
535
	BC_ASSERT_PTR_NOT_NULL(c);
536

537 538
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
539 540 541
	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);
542 543

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
544
	linphone_carddav_put_vcard(c, lf);
545
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
546
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
547
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
548

549
	linphone_carddav_delete_vcard(c, lf);
550
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
551 552 553 554
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");

	linphone_friend_unref(lf);
	ms_free(stats);
555 556 557 558 559 560 561 562 563 564 565 566 567 568
	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++;
}

569 570
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);
571
	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)));
572 573 574
	stats->updated_contact_count++;
}

575 576 577 578 579 580 581 582 583
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++;
	}
}

584
static void carddav_integration(void) {
585
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
586
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
587
	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");
588
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
589
	LinphoneVcard *lvc2 = NULL;
590
	LinphoneFriend *lf2 = NULL;
591 592
	LinphoneFriendListCbs *cbs = NULL;
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
593
	const char *refkey = "toto";
594
	char *address = NULL;
595
	LinphoneAddress *addr;
596

597
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
598 599 600 601
	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);
602
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
603
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
604
	linphone_core_add_friend_list(manager->lc, lfl);
605 606

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
607
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
608
	BC_ASSERT_EQUAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
609
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->dirty_friends_to_update), 1, int, "%d");
610
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
611
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
612
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
613
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
614
	linphone_friend_list_remove_friend(lfl, lf);
615
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->friends), 0, int, "%d");
616
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
617
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
618
	linphone_friend_unref(lf);
619
	lf = NULL;
620

621
	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");
622
	lf = linphone_friend_new_from_vcard(lvc);
623
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
624
	linphone_friend_unref(lf);
625

626
	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");
627
	linphone_vcard_set_url(lvc2, "/card.php/addressbooks/tester/default/me.vcf");
628
	lf2 = linphone_friend_new_from_vcard(lvc2);
629
	linphone_friend_set_ref_key(lf2, refkey);
630
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf2), LinphoneFriendListOK, int, "%d");
631

632 633
	BC_ASSERT_EQUAL(lfl->revision, 0, int, "%i");
	linphone_friend_list_synchronize_friends_from_server(lfl);
634
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 0, 5000);
635
	BC_ASSERT_EQUAL(stats->new_contact_count, 0, int, "%i");
636
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 5000);
637
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
638
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 5000);
639
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
640
	BC_ASSERT_NOT_EQUAL(lfl->revision, 0, int, "%i");
641
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 5000);
642
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
643

644
	BC_ASSERT_EQUAL(bctbx_list_size(lfl->friends), 1, int, "%i");
645
	lf = (LinphoneFriend *)lfl->friends->data;
646
	BC_ASSERT_STRING_EQUAL(lf->refkey, refkey);
647
	BC_ASSERT_EQUAL(lf->storage_id, lf2->storage_id, unsigned int, "%u");
648
	linphone_friend_unref(lf2);
649 650
	addr = linphone_friend_get_address(lf);
	address = linphone_address_as_string_uri_only(addr);
651 652
	BC_ASSERT_STRING_EQUAL(address, "sip:sylvain@sip.linphone.org");
	ms_free(address);
653