vcard_tester.c 29.4 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

	start = clock();
69
	linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
70 71
	end = clock();
	
72
	friends = linphone_friend_list_get_friends(lfl);
73 74
	BC_ASSERT_EQUAL(ms_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
	
75
	elapsed = (double)(end - start);
76
	ms_error("Imported a thousand of vCards (only %i friends with SIP address found) in %f seconds", ms_list_size(friends), elapsed / CLOCKS_PER_SEC);
77
#ifndef ANDROID
78
	BC_ASSERT_TRUE(elapsed < 1500000); // 1.5 seconds
79
#endif
80 81
	
	ms_free(import_filepath);
82 83 84
	linphone_core_manager_destroy(manager);
}

85 86 87 88 89
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;
90
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
91 92 93
	
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
94 95
	linphone_friend_list_add_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(lfl);
96 97
	BC_ASSERT_EQUAL(ms_list_size(friends), 1, int, "%d");
	
98 99 100
	linphone_friend_list_remove_friend(lfl, lf);
	linphone_friend_unref(lf);
	friends = linphone_friend_list_get_friends(lfl);
101 102
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
103
	linphone_friend_list_unref(lfl);
104
	linphone_address_unref(addr);
105 106 107 108 109
	linphone_core_manager_destroy(manager);
}

#ifdef FRIENDS_SQL_STORAGE_ENABLED
static void friends_migration(void) {
110
	LinphoneCoreManager* manager = linphone_core_manager_new2("friends_rc", FALSE);
111 112
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
	const MSList *friends = linphone_friend_list_get_friends(lfl);
113 114 115 116 117 118
	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);
119
	friends = linphone_friend_list_get_friends(lfl);
120
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
121
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc, lfl);
122
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 3, int, "%d");
123
	
124
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
125 126 127 128 129
	unlink(friends_db);
	ms_free(friends_db);
	linphone_core_manager_destroy(manager);
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
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++;
}

145
static void friends_sqlite_storage(void) {
146 147 148
	LinphoneCoreVTable *v_table = linphone_core_v_table_new();
	LinphoneCore* lc = NULL;
	LinphoneFriendList *lfl = NULL;
149
	LinphoneFriend *lf = NULL;
150
	LinphoneFriend *lf2 = NULL;
151
	LinphoneVCard *lvc = linphone_vcard_new();
152
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
153
	const MSList *friends = NULL;
154
	MSList *friends_from_db = NULL;
155
	MSList *friends_lists_from_db = NULL;
156
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
157 158 159 160 161
	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);
162
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
163 164
	lfl = linphone_core_create_friend_list(lc);
	linphone_friend_list_set_user_data(lfl, stats);
165 166 167
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
	unlink(friends_db);
168 169
	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));
170
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");
171
	
172 173
	linphone_vcard_set_etag(lvc, "\"123-456789\"");
	linphone_vcard_set_url(lvc, "http://dav.somewhere.fr/addressbook/me/someone.vcf");
174
	lf = linphone_friend_new_from_vcard(lvc);
175 176
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
177
	
178 179 180
	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");
181
	linphone_friend_list_unref(lfl);
182
	linphone_friend_list_set_display_name(lfl, "Test");
183
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%i");
184
	linphone_friend_unref(lf);
185
	BC_ASSERT_EQUAL(lfl->storage_id, 1, int, "%d");
186
	BC_ASSERT_EQUAL(lf->storage_id, 1, int, "%d");
187
	
188
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
189 190
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
191
	friends_lists_from_db = linphone_core_fetch_friends_lists_from_db(lc);
192
	BC_ASSERT_EQUAL(ms_list_size(friends_lists_from_db), 1, int, "%d");
193 194 195 196 197
	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);
198
	friends_lists_from_db = ms_list_free_with_data(friends_lists_from_db, (void (*)(void *))linphone_friend_list_unref);
199
	
200
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
201
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
202 203 204 205 206 207
	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");
208 209
	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)));
210 211 212 213 214 215
	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);
216
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
217 218 219 220
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
	if (ms_list_size(friends_from_db) < 1) {
		goto end;
	}
221
	lf2 = (LinphoneFriend *)friends_from_db->data;
222 223
	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);
224
	
225 226
	linphone_friend_list_remove_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
227
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
228
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
229
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");
230 231 232 233
	
	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");
234 235

end:
236 237
	unlink(friends_db);
	ms_free(friends_db);
238
	linphone_address_unref(addr);
239
	linphone_core_destroy(lc);
240 241
}
#endif
242

243 244 245 246 247 248 249
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

250
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
251
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
252
	BC_ASSERT_TRUE(success);
Sylvain Berfini's avatar
Sylvain Berfini committed
253
	stats->sync_done_count++;
254 255
}

256 257 258
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
259
	stats->new_contact_count++;
260 261 262 263 264
}

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
265
	stats->removed_contact_count++;
266 267
}

268
static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *new_lf, LinphoneFriend *old_lf) {
269
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
270 271
	BC_ASSERT_PTR_NOT_NULL_FATAL(new_lf);
	BC_ASSERT_PTR_NOT_NULL_FATAL(old_lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
272
	stats->updated_contact_count++;
273 274
}

275
static void carddav_sync(void) {
276
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
277
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
278 279
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
280
	
281
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
282
	linphone_core_add_friend_list(manager->lc, lfl);
283
	linphone_friend_list_unref(lfl);
284
	c = linphone_carddav_context_new(lfl);
285 286
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	
287
	linphone_carddav_set_user_data(c, stats);
288
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
289 290 291
	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);
292 293
	linphone_carddav_synchronize(c);
	
294
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 2000);
Sylvain Berfini's avatar
Sylvain Berfini committed
295
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
296
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
Sylvain Berfini's avatar
Sylvain Berfini committed
297
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
298 299
	
	ms_free(stats);
300
	linphone_carddav_context_destroy(c);
301 302
	linphone_core_manager_destroy(manager);
}
Sylvain Berfini's avatar
Sylvain Berfini committed
303 304

static void carddav_sync_2(void) {
305
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
306 307 308
	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");
309 310 311 312
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
	
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
313
	linphone_core_add_friend_list(manager->lc, lfl);
314
	linphone_friend_list_unref(lfl);
315 316
	c = linphone_carddav_context_new(lfl);
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
317 318 319
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
320
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	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
337 338 339 340
	
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
341
	linphone_carddav_context_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
342 343 344 345
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
346
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
347
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
348
	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
349 350
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
351 352 353 354
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
	
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
355
	linphone_core_add_friend_list(manager->lc, lfl);
356
	linphone_friend_list_unref(lfl);
357 358
	c = linphone_carddav_context_new(lfl);
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
359 360 361
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
362
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376
	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
377 378 379 380

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
381
	linphone_carddav_context_destroy(c);
382 383 384 385
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_4(void) {
386
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
387 388 389
	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);
390 391
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
392
	
393
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
394
	linphone_core_add_friend_list(manager->lc, lfl);
395
	linphone_friend_list_unref(lfl);
396
	c = linphone_carddav_context_new(lfl);
397 398 399 400
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
401 402 403
	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);
404
	
405
	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));	
406
	linphone_carddav_put_vcard(c, lf);
407
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
408 409 410 411 412 413 414 415 416
	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);
417 418 419 420 421 422 423 424 425 426 427 428 429 430
	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++;
}

431 432
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);
433
	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)));
434 435 436
	stats->updated_contact_count++;
}

437 438 439 440 441 442 443 444 445
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++;
	}
}

446
static void carddav_integration(void) {
447
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
448 449 450
	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);
451 452
	LinphoneVCard *lvc2 = NULL;
	LinphoneFriend *lf2 = NULL;
453 454
	LinphoneFriendListCbs *cbs = NULL;
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
455
	const char *refkey = "toto";
456 457 458 459 460 461
	
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
	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);
462
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
463
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
464
	linphone_core_add_friend_list(manager->lc, lfl);
465 466

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
467
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
468
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
469
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 1, int, "%d");
470 471
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
472 473
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
474
	linphone_friend_list_remove_friend(lfl, lf);
475
	BC_ASSERT_EQUAL(ms_list_size(lfl->friends), 0, int, "%d");
476 477
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
478
	linphone_friend_unref(lf);
479
	lf = NULL;
480 481 482

	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);
483
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
484
	linphone_friend_unref(lf);
485
	
486
	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");
487 488
	linphone_vcard_set_url(lvc2, "/sabredav/addressbookserver.php/addressbooks/sylvain/default/me.vcf");
	lf2 = linphone_friend_new_from_vcard(lvc2);
489
	linphone_friend_set_ref_key(lf2, refkey);
490
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_local_friend(lfl, lf2), LinphoneFriendListOK, int, "%d");
491
	
492 493
	BC_ASSERT_EQUAL(lfl->revision, 0, int, "%i");
	linphone_friend_list_synchronize_friends_from_server(lfl);
494 495
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 0, 2000);
	BC_ASSERT_EQUAL(stats->new_contact_count, 0, int, "%i");
496 497
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
498 499
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
500
	BC_ASSERT_NOT_EQUAL(lfl->revision, 0, int, "%i");
501 502
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
503
	
504 505
	BC_ASSERT_EQUAL_FATAL(ms_list_size(lfl->friends), 1, int, "%i");
	lf = (LinphoneFriend *)lfl->friends->data;
506 507
	BC_ASSERT_STRING_EQUAL(lf->refkey, refkey);
	BC_ASSERT_EQUAL(lf->storage_id, lf2->storage_id, int, "%i");
508
	linphone_friend_unref(lf2);
509 510
	BC_ASSERT_STRING_EQUAL(linphone_address_as_string_uri_only(lf->uri), "sip:sylvain@sip.linphone.org");
	
511 512 513 514 515 516 517 518 519 520
	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");
	
521 522
	ms_free(stats);
	linphone_friend_list_unref(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
523 524 525
	linphone_core_manager_destroy(manager);
}

526 527 528 529 530 531 532 533 534 535 536 537 538
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);
539
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
540 541 542 543
	linphone_core_add_friend_list(manager->lc, lfl);
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
	
	linphone_friend_list_synchronize_friends_from_server(lfl);
544 545 546
	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;
547 548 549 550 551
	
	friends = ms_list_copy(lfl->friends);
	while (friends) {
		LinphoneFriend *lf = (LinphoneFriend *)friends->data;
		linphone_friend_list_remove_friend(lfl, lf);
552 553 554
		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;
555 556 557 558 559 560 561 562
		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");
	linphone_vcard_set_url(lvc, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default/me.vcf");
	lf = linphone_friend_new_from_vcard(lvc);
	linphone_friend_list_add_friend(lfl, lf);
563 564
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
565 566 567 568 569 570 571
	
	ms_free(stats);
	linphone_friend_unref(lf);
	linphone_friend_list_unref(lfl);
	linphone_core_manager_destroy(manager);
}

572 573 574 575 576 577 578 579 580 581
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);
582
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
583 584 585 586
	linphone_core_add_friend_list(manager->lc, lfl);
	linphone_friend_list_set_uri(lfl, "http://192.168.0.230/sabredav/addressbookserver.php/addressbooks/sylvain/default");
	
	linphone_friend_list_synchronize_friends_from_server(lfl);
587 588
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
589
	linphone_friend_list_synchronize_friends_from_server(lfl);
590 591
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
592
	linphone_friend_list_synchronize_friends_from_server(lfl);
593 594
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 2000);
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
595 596 597 598 599 600
	BC_ASSERT_EQUAL(stats->removed_contact_count, 0, int, "%i");
	
	linphone_friend_list_unref(lfl);
	linphone_core_manager_destroy(manager);
}

601 602 603
#else
static void dummy_test(void) {
}
604
#endif
605

606
test_t vcard_tests[] = {
607
#ifdef VCARD_ENABLED
Sylvain Berfini's avatar
Sylvain Berfini committed
608
	{ "Import / Export friends from vCards", linphone_vcard_import_export_friends_test },
609
	{ "Import a lot of friends from vCards", linphone_vcard_import_a_lot_of_friends_test },
610 611 612 613
#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 },
614
#endif
615
	{ "CardDAV clean", carddav_clean }, // This is to ensure the content of the test addressbook is in the correct state for the following tests
616
	{ "CardDAV synchronization", carddav_sync },
Sylvain Berfini's avatar
Sylvain Berfini committed
617 618
	{ "CardDAV synchronization 2", carddav_sync_2 },
	{ "CardDAV synchronization 3", carddav_sync_3 },
619
	{ "CardDAV synchronization 4", carddav_sync_4 },
620
	{ "CardDAV integration", carddav_integration },
621
	{ "CardDAV multiple synchronizations", carddav_multiple_sync },
622 623
#else
	{ "Dummy test", dummy_test }
624
#endif
625 626 627 628 629 630 631
};

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
};