vcard_tester.c 31.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
	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/>.
*/

#include "linphonecore.h"
#include "private.h"
#include "liblinphone_tester.h"
22
#include "carddav.h"
23

24 25
#include <time.h>

26
#ifdef VCARD_ENABLED
Sylvain Berfini's avatar
Sylvain Berfini committed
27 28 29 30 31
static char *create_filepath(const char *dir, const char *filename, const char *ext) {
	return ms_strdup_printf("%s/%s.%s", dir, filename, ext);
}

static void linphone_vcard_import_export_friends_test(void) {
32
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
33 34
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
	const MSList *friends = linphone_friend_list_get_friends(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
35 36
	char *import_filepath = bc_tester_res("common/vcards.vcf");
	char *export_filepath = create_filepath(bc_tester_get_writable_dir_prefix(), "export_vcards", "vcf");
37 38 39
	int count = 0;
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
40
	count = linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
41
	BC_ASSERT_EQUAL(count, 3, int, "%d");
42
	friends = linphone_friend_list_get_friends(lfl);
43 44
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
	
45
	linphone_friend_list_export_friends_as_vcard4_file(lfl, export_filepath);
46
	
47 48
	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
49
	BC_ASSERT_EQUAL(count, 3, int, "%d");
50
	friends = linphone_friend_list_get_friends(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
51
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
52
	linphone_friend_list_unref(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
53 54
	
	remove(export_filepath);
55 56
	ms_free(import_filepath);
	ms_free(export_filepath);
Sylvain Berfini's avatar
Sylvain Berfini committed
57
	linphone_core_manager_destroy(manager);
58 59
}

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

	start = clock();
72
	linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
73 74
	end = clock();
	
75
	friends = linphone_friend_list_get_friends(lfl);
76 77
	BC_ASSERT_EQUAL(ms_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
	
78
	elapsed = (double)(end - start);
79
	ms_error("Imported a thousand of vCards from file (only %i friends with SIP address found) in %f seconds", ms_list_size(friends), elapsed / CLOCKS_PER_SEC);
80
#ifndef ANDROID
81
	BC_ASSERT_TRUE(elapsed < 1500000); // 1.5 seconds
82
#endif
83
	
84
	lfl = linphone_core_create_friend_list(manager->lc);
85 86 87 88 89 90 91 92 93
	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);
		buffer = (char*)ms_malloc(numbytes * sizeof(char));
		numbytes = fread(buffer, sizeof(char), numbytes, infile);
		fclose(infile);
94
	
95 96 97 98 99
		start = clock();
		linphone_friend_list_import_friends_from_vcard4_buffer(lfl, buffer);
		end = clock();
		ms_free(buffer);
	}
100 101 102 103 104 105 106 107 108 109 110 111
	
	friends = linphone_friend_list_get_friends(lfl);
	BC_ASSERT_EQUAL(ms_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
	
	elapsed = (double)(end - start);
	ms_error("Imported a thousand of vCards from buffer (only %i friends with SIP address found) in %f seconds", ms_list_size(friends), elapsed / CLOCKS_PER_SEC);
#ifndef ANDROID
	BC_ASSERT_TRUE(elapsed < 1500000); // 1.5 seconds
#endif
	
	linphone_friend_list_unref(lfl);
	
112
	ms_free(import_filepath);
113 114 115
	linphone_core_manager_destroy(manager);
}

116 117 118 119 120 121 122 123 124 125 126 127
static void linphone_vcard_update_existing_friends_test(void) {
	LinphoneFriend *lf = linphone_friend_new_with_addr("sip:oldfriend@sip.linphone.org");
	
	BC_ASSERT_PTR_NOT_NULL_FATAL(lf);
	BC_ASSERT_PTR_NULL(linphone_friend_get_vcard(lf));

	linphone_friend_edit(lf);
	linphone_friend_set_name(lf, "Old Friend");
	linphone_friend_done(lf);
	
	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
128 129
	linphone_friend_unref(lf);
	lf = NULL;
130 131
}

132 133 134 135 136
static void friends_if_no_db_set(void) {
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
	LinphoneFriend *lf = linphone_friend_new();
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	const MSList *friends = NULL;
137
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
138 139 140
	
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
141 142
	linphone_friend_list_add_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(lfl);
143 144
	BC_ASSERT_EQUAL(ms_list_size(friends), 1, int, "%d");
	
145 146 147
	linphone_friend_list_remove_friend(lfl, lf);
	linphone_friend_unref(lf);
	friends = linphone_friend_list_get_friends(lfl);
148 149
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
150
	linphone_friend_list_unref(lfl);
151
	linphone_address_unref(addr);
152 153 154 155 156
	linphone_core_manager_destroy(manager);
}

#ifdef FRIENDS_SQL_STORAGE_ENABLED
static void friends_migration(void) {
157
	LinphoneCoreManager* manager = linphone_core_manager_new2("friends_rc", FALSE);
158 159
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
	const MSList *friends = linphone_friend_list_get_friends(lfl);
160 161 162 163 164 165
	MSList *friends_from_db = NULL;
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
166
	friends = linphone_friend_list_get_friends(lfl);
167
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
168
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc, lfl);
169
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 3, int, "%d");
170
	
171
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
172 173 174 175 176
	unlink(friends_db);
	ms_free(friends_db);
	linphone_core_manager_destroy(manager);
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
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);
	stats->new_list_count++;
}

static void friend_list_removed_cb(LinphoneCore *lc, LinphoneFriendList *list) {
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)linphone_friend_list_get_user_data(list);
	stats->removed_list_count++;
}

192
static void friends_sqlite_storage(void) {
193 194 195
	LinphoneCoreVTable *v_table = linphone_core_v_table_new();
	LinphoneCore* lc = NULL;
	LinphoneFriendList *lfl = NULL;
196
	LinphoneFriend *lf = NULL;
197
	LinphoneFriend *lf2 = NULL;
198
	LinphoneVCard *lvc = linphone_vcard_new();
199
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
200
	const MSList *friends = NULL;
201
	MSList *friends_from_db = NULL;
202
	MSList *friends_lists_from_db = NULL;
203
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
204 205 206 207 208
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)ms_new0(LinphoneFriendListStats, 1);
	
	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);
209
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
210 211
	lfl = linphone_core_create_friend_list(lc);
	linphone_friend_list_set_user_data(lfl, stats);
212 213 214
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
	unlink(friends_db);
215 216
	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));
217
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");
218
	
219 220
	linphone_vcard_set_etag(lvc, "\"123-456789\"");
	linphone_vcard_set_url(lvc, "http://dav.somewhere.fr/addressbook/me/someone.vcf");
221
	lf = linphone_friend_new_from_vcard(lvc);
222 223
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
224
	
225 226 227
	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");
228
	linphone_friend_list_unref(lfl);
229
	linphone_friend_list_set_display_name(lfl, "Test");
230
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%i");
231
	linphone_friend_unref(lf);
232
	BC_ASSERT_EQUAL(lfl->storage_id, 1, int, "%d");
233
	BC_ASSERT_EQUAL(lf->storage_id, 1, int, "%d");
234
	
235
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
236 237
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
238
	friends_lists_from_db = linphone_core_fetch_friends_lists_from_db(lc);
239
	BC_ASSERT_EQUAL(ms_list_size(friends_lists_from_db), 1, int, "%d");
240 241 242 243 244
	friends_from_db = ((LinphoneFriendList *)friends_lists_from_db->data)->friends;
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
	lf2 = (LinphoneFriend *)friends_from_db->data;
	BC_ASSERT_PTR_NOT_NULL(lf2->lc);
	BC_ASSERT_PTR_NOT_NULL(lf2->friend_list);
245
	friends_lists_from_db = ms_list_free_with_data(friends_lists_from_db, (void (*)(void *))linphone_friend_list_unref);
246
	
247
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
248
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
249 250 251 252 253 254
	if (ms_list_size(friends_from_db) < 1) {
		goto end;
	}
	lf2 = (LinphoneFriend *)friends_from_db->data;
	BC_ASSERT_STRING_EQUAL(linphone_friend_get_name(lf2), linphone_friend_get_name(lf));
	BC_ASSERT_EQUAL(lf2->storage_id, lf->storage_id, int, "%i");
255 256
	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)));
257 258 259 260 261 262
	BC_ASSERT_STRING_EQUAL(linphone_address_as_string(linphone_friend_get_address(lf2)), linphone_address_as_string(linphone_friend_get_address(lf)));

	linphone_friend_edit(lf);
	linphone_friend_set_name(lf, "Margaux");
	linphone_friend_done(lf);
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
263
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
264 265 266 267
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
	if (ms_list_size(friends_from_db) < 1) {
		goto end;
	}
268
	lf2 = (LinphoneFriend *)friends_from_db->data;
269 270
	BC_ASSERT_STRING_EQUAL(linphone_friend_get_name(lf2), "Margaux");
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
271
	
272 273
	linphone_friend_list_remove_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
274
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
275
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
276
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");
277 278 279 280
	
	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");
281 282

end:
283 284
	unlink(friends_db);
	ms_free(friends_db);
285
	linphone_address_unref(addr);
286
	linphone_core_destroy(lc);
287 288
}
#endif
289

290 291 292 293 294 295 296
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

297
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
298
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
299
	BC_ASSERT_TRUE(success);
Sylvain Berfini's avatar
Sylvain Berfini committed
300
	stats->sync_done_count++;
301 302
}

303 304 305
static void carddav_new_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
	BC_ASSERT_PTR_NOT_NULL_FATAL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
306
	stats->new_contact_count++;
307 308 309 310 311
}

static void carddav_removed_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
	BC_ASSERT_PTR_NOT_NULL_FATAL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
312
	stats->removed_contact_count++;
313 314
}

315
static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *new_lf, LinphoneFriend *old_lf) {
316
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
317 318
	BC_ASSERT_PTR_NOT_NULL_FATAL(new_lf);
	BC_ASSERT_PTR_NOT_NULL_FATAL(old_lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
319
	stats->updated_contact_count++;
320 321
}

322
static void carddav_sync(void) {
323
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
324
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
325 326
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
327
	
328
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
329
	linphone_core_add_friend_list(manager->lc, lfl);
330
	linphone_friend_list_unref(lfl);
331
	c = linphone_carddav_context_new(lfl);
332 333
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	
334
	linphone_carddav_set_user_data(c, stats);
335
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
336 337 338
	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);
339 340
	linphone_carddav_synchronize(c);
	
341
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 2000);
Sylvain Berfini's avatar
Sylvain Berfini committed
342
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
343
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
Sylvain Berfini's avatar
Sylvain Berfini committed
344
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
345 346
	
	ms_free(stats);
347
	linphone_carddav_context_destroy(c);
348 349
	linphone_core_manager_destroy(manager);
}
Sylvain Berfini's avatar
Sylvain Berfini committed
350 351

static void carddav_sync_2(void) {
352
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
353 354 355
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
	LinphoneFriend *lf = linphone_friend_new_with_address("\"Sylvain\" <sip:sylvain@sip.linphone.org>");
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
356 357 358
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
	
359
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
360
	linphone_core_add_friend_list(manager->lc, lfl);
361
	linphone_friend_list_unref(lfl);
362 363
	c = linphone_carddav_context_new(lfl);
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
364 365 366
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
367
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	linphone_friend_unref(lf);
	
	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);
	
	linphone_carddav_synchronize(c);
	
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
384 385 386 387
	
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
388
	linphone_carddav_context_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
389 390 391 392
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
393
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
394
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
395
	LinphoneVCard *lvc = linphone_vcard_new_from_vcard4_buffer("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
396 397
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
398 399 400
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
	
401
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
402
	linphone_core_add_friend_list(manager->lc, lfl);
403
	linphone_friend_list_unref(lfl);
404 405
	c = linphone_carddav_context_new(lfl);
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
406 407 408
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
409
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423
	linphone_friend_unref(lf);
	
	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);
	
	linphone_carddav_synchronize(c);
	
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
424 425 426 427

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
428
	linphone_carddav_context_destroy(c);
429 430 431 432
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_4(void) {
433
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
434 435 436
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
	LinphoneVCard *lvc = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Margaux Clerc\r\nIMPP;TYPE=work:sip:margaux@sip.linphone.org\r\nEND:VCARD\r\n");
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
437 438
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
439
	
440
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
441
	linphone_core_add_friend_list(manager->lc, lfl);
442
	linphone_friend_list_unref(lfl);
443
	c = linphone_carddav_context_new(lfl);
444 445 446 447
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
448 449 450
	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);
451
	
452
	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));	
453
	linphone_carddav_put_vcard(c, lf);
454
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
455 456 457 458 459 460 461 462 463
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
	
	linphone_carddav_delete_vcard(c, lf);
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");

	linphone_friend_unref(lf);
	ms_free(stats);
464 465 466 467 468 469 470 471 472 473 474 475 476 477
	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++;
}

478 479
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);
480
	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)));
481 482 483
	stats->updated_contact_count++;
}

484 485 486 487 488 489 490 491 492
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++;
	}
}

493
static void carddav_integration(void) {
494
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
495 496 497
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneVCard *lvc = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Margaux Clerc\r\nIMPP;TYPE=work:sip:margaux@sip.linphone.org\r\nEND:VCARD\r\n");
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
498 499
	LinphoneVCard *lvc2 = NULL;
	LinphoneFriend *lf2 = NULL;
500 501
	LinphoneFriendListCbs *cbs = NULL;
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
502
	const char *refkey = "toto";
503
	
504
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
505 506 507 508
	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);
509
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
510
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
511
	linphone_core_add_friend_list(manager->lc, lfl);
512 513

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
514
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
515
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
516
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 1, int, "%d");
517 518
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
519 520
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
521
	linphone_friend_list_remove_friend(lfl, lf);
522
	BC_ASSERT_EQUAL(ms_list_size(lfl->friends), 0, int, "%d");
523 524
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
525
	linphone_friend_unref(lf);
526
	lf = NULL;
527 528 529

	lvc = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Ghislain Mary\r\nIMPP;TYPE=work:sip:ghislain@sip.linphone.org\r\nEND:VCARD\r\n");
	lf = linphone_friend_new_from_vcard(lvc);
530
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
531
	linphone_friend_unref(lf);
532
	
533
	lvc2 = linphone_vcard_new_from_vcard4_buffer("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");
534 535
	linphone_vcard_set_url(lvc2, "/sabredav/addressbookserver.php/addressbooks/sylvain/default/me.vcf");
	lf2 = linphone_friend_new_from_vcard(lvc2);
536
	linphone_friend_set_ref_key(lf2, refkey);
537
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_local_friend(lfl, lf2), LinphoneFriendListOK, int, "%d");
538
	
539 540
	BC_ASSERT_EQUAL(lfl->revision, 0, int, "%i");
	linphone_friend_list_synchronize_friends_from_server(lfl);
541 542
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 0, 2000);
	BC_ASSERT_EQUAL(stats->new_contact_count, 0, int, "%i");
543 544
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
545 546
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
547
	BC_ASSERT_NOT_EQUAL(lfl->revision, 0, int, "%i");
548 549
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
550
	
551 552
	BC_ASSERT_EQUAL_FATAL(ms_list_size(lfl->friends), 1, int, "%i");
	lf = (LinphoneFriend *)lfl->friends->data;
553 554
	BC_ASSERT_STRING_EQUAL(lf->refkey, refkey);
	BC_ASSERT_EQUAL(lf->storage_id, lf2->storage_id, int, "%i");
555
	linphone_friend_unref(lf2);
556 557
	BC_ASSERT_STRING_EQUAL(linphone_address_as_string_uri_only(lf->uri), "sip:sylvain@sip.linphone.org");
	
558 559 560 561 562 563 564 565 566 567
	linphone_friend_edit(lf);
	linphone_friend_done(lf);
	BC_ASSERT_EQUAL(ms_list_size(lf->friend_list->dirty_friends_to_update), 0, int, "%i");
	
	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);
	BC_ASSERT_EQUAL(ms_list_size(lf->friend_list->dirty_friends_to_update), 1, int, "%i");
	
568 569
	ms_free(stats);
	linphone_friend_list_unref(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
570 571 572
	linphone_core_manager_destroy(manager);
}

573 574 575 576 577 578 579 580 581 582 583 584 585
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);
	MSList *friends = NULL;
	LinphoneFriend *lf = NULL;
	LinphoneVCard *lvc = NULL;
	
	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);
586
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
587
	linphone_core_add_friend_list(manager->lc, lfl);
588
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
589 590
	
	linphone_friend_list_synchronize_friends_from_server(lfl);
591 592 593
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
	stats->sync_done_count = 0;
594 595 596 597 598
	
	friends = ms_list_copy(lfl->friends);
	while (friends) {
		LinphoneFriend *lf = (LinphoneFriend *)friends->data;
		linphone_friend_list_remove_friend(lfl, lf);
599 600 601
		wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
		BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
		stats->sync_done_count = 0;
602 603 604 605 606
		stats->removed_contact_count = 0;
		friends = ms_list_next(friends);
	}
	
	lvc = linphone_vcard_new_from_vcard4_buffer("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");
607
	linphone_vcard_set_url(lvc, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default/me.vcf");
608 609
	lf = linphone_friend_new_from_vcard(lvc);
	linphone_friend_list_add_friend(lfl, lf);
610 611
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
612 613 614 615 616 617 618
	
	ms_free(stats);
	linphone_friend_unref(lf);
	linphone_friend_list_unref(lfl);
	linphone_core_manager_destroy(manager);
}

619 620 621 622 623 624 625 626 627 628
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);
	
	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);
629
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
630
	linphone_core_add_friend_list(manager->lc, lfl);
631
	linphone_friend_list_set_uri(lfl, "http://dav.linphone.org/sabredav/addressbookserver.php/addressbooks/sylvain/default");
632 633
	
	linphone_friend_list_synchronize_friends_from_server(lfl);
634 635
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
636
	linphone_friend_list_synchronize_friends_from_server(lfl);
637 638
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
639
	linphone_friend_list_synchronize_friends_from_server(lfl);
640 641
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
642 643 644 645 646 647
	BC_ASSERT_EQUAL(stats->removed_contact_count, 0, int, "%i");
	
	linphone_friend_list_unref(lfl);
	linphone_core_manager_destroy(manager);
}

648 649 650
#else
static void dummy_test(void) {
}
651
#endif
652

653
test_t vcard_tests[] = {
654
#ifdef VCARD_ENABLED
Sylvain Berfini's avatar
Sylvain Berfini committed
655
	{ "Import / Export friends from vCards", linphone_vcard_import_export_friends_test },
656
	{ "Import a lot of friends from vCards", linphone_vcard_import_a_lot_of_friends_test },
657
	{ "vCard creation for existing friends", linphone_vcard_update_existing_friends_test },
658 659 660 661
#ifdef FRIENDS_SQL_STORAGE_ENABLED
	{ "Friends working if no db set", friends_if_no_db_set },
	{ "Friends storage migration from rc to db", friends_migration },
	{ "Friends storage in sqlite database", friends_sqlite_storage },
662
#endif
663
	{ "CardDAV clean", carddav_clean }, // This is to ensure the content of the test addressbook is in the correct state for the following tests
664
	{ "CardDAV synchronization", carddav_sync },
Sylvain Berfini's avatar
Sylvain Berfini committed
665 666
	{ "CardDAV synchronization 2", carddav_sync_2 },
	{ "CardDAV synchronization 3", carddav_sync_3 },
667
	{ "CardDAV synchronization 4", carddav_sync_4 },
668
	{ "CardDAV integration", carddav_integration },
669
	{ "CardDAV multiple synchronizations", carddav_multiple_sync },
670 671
#else
	{ "Dummy test", dummy_test }
672
#endif
673 674 675 676 677 678 679
};

test_suite_t vcard_test_suite = {
	"VCard", NULL, NULL, 
	liblinphone_tester_before_each, liblinphone_tester_after_each,
	sizeof(vcard_tests) / sizeof(vcard_tests[0]), vcard_tests
};