vcard_tester.c 24.1 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);
Sylvain Berfini's avatar
Sylvain Berfini committed
33 34
	char *import_filepath = bc_tester_res("common/vcards.vcf");
	char *export_filepath = create_filepath(bc_tester_get_writable_dir_prefix(), "export_vcards", "vcf");
35 36
	const MSList *friends = linphone_core_get_friend_list(manager->lc);
	int count = 0;
37
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
38 39
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
40
	BC_ASSERT_PTR_NOT_NULL_FATAL(linphone_core_get_default_friend_list(manager->lc));
Sylvain Berfini's avatar
Sylvain Berfini committed
41
	count = linphone_core_import_friends_from_vcard4_file(manager->lc, import_filepath);
42 43 44 45
	BC_ASSERT_EQUAL(count, 3, int, "%d");
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
	
Sylvain Berfini's avatar
Sylvain Berfini committed
46
	linphone_core_export_friends_as_vcard4_file(manager->lc, export_filepath);
47
	
48
	linphone_core_remove_friend_list(manager->lc, linphone_core_get_default_friend_list(manager->lc));
Sylvain Berfini's avatar
Sylvain Berfini committed
49 50
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
51
	
52 53 54
	linphone_core_add_friend_list(manager->lc, lfl);
	linphone_friend_list_unref(lfl);
	lfl = NULL;
Sylvain Berfini's avatar
Sylvain Berfini committed
55 56 57 58 59 60
	count = linphone_core_import_friends_from_vcard4_file(manager->lc, export_filepath);
	BC_ASSERT_EQUAL(count, 3, int, "%d");
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
	
	remove(export_filepath);
61 62
	ms_free(import_filepath);
	ms_free(export_filepath);
Sylvain Berfini's avatar
Sylvain Berfini committed
63
	linphone_core_manager_destroy(manager);
64 65
}

66 67 68 69 70
static void linphone_vcard_import_a_lot_of_friends_test(void) {
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
	char *import_filepath = bc_tester_res("common/thousand_vcards.vcf");
	clock_t start, end;
	double elapsed = 0;
71
	const MSList *friends = NULL;
72 73 74 75 76

	start = clock();
	linphone_core_import_friends_from_vcard4_file(manager->lc, import_filepath);
	end = clock();
	
77
	friends = linphone_core_get_friend_list(manager->lc);
78 79
	BC_ASSERT_EQUAL(ms_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
	
80
	elapsed = (double)(end - start);
81
	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);
82
#ifndef ANDROID
83
	BC_ASSERT_TRUE(elapsed < 1500000); // 1.5 seconds
84
#endif
85 86
	
	ms_free(import_filepath);
87 88 89
	linphone_core_manager_destroy(manager);
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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;
	
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
	linphone_core_add_friend(manager->lc, lf);
	linphone_friend_unref(lf);
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 1, int, "%d");
	
	linphone_core_remove_friend(manager->lc, lf);
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
107
	linphone_address_unref(addr);
108 109 110 111 112
	linphone_core_manager_destroy(manager);
}

#ifdef FRIENDS_SQL_STORAGE_ENABLED
static void friends_migration(void) {
113
	LinphoneCoreManager* manager = linphone_core_manager_new2("friends_rc", FALSE);
114 115 116 117 118 119 120
	const MSList *friends = linphone_core_get_friend_list(manager->lc);
	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);
121 122
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
123
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc, linphone_core_get_default_friend_list(manager->lc));
124 125 126 127
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 3, int, "%d");
	if (ms_list_size(friends_from_db) < 3) {
		goto end;
	}
128
	
129
end:
130 131 132 133 134 135
	unlink(friends_db);
	ms_free(friends_db);
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
	linphone_core_manager_destroy(manager);
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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++;
}

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

end:
242 243
	unlink(friends_db);
	ms_free(friends_db);
244
	linphone_address_unref(addr);
245
	linphone_core_destroy(lc);
246 247
}
#endif
248

249 250 251 252 253 254 255
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

256
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
257
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
258
	BC_ASSERT_TRUE(success);
Sylvain Berfini's avatar
Sylvain Berfini committed
259
	stats->sync_done_count++;
260 261
}

262 263 264
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
265
	stats->new_contact_count++;
266 267 268 269 270
}

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
271
	stats->removed_contact_count++;
272 273
}

274
static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *new_lf, LinphoneFriend *old_lf) {
275
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
276 277
	BC_ASSERT_PTR_NOT_NULL_FATAL(new_lf);
	BC_ASSERT_PTR_NOT_NULL_FATAL(old_lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
278
	stats->updated_contact_count++;
279 280
}

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

static void carddav_sync_2(void) {
311
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
312 313 314
	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");
315 316 317 318
	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");
319
	linphone_core_add_friend_list(manager->lc, lfl);
320
	linphone_friend_list_unref(lfl);
321 322
	c = linphone_carddav_context_new(lfl);
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
323 324 325
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
326
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	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
343 344 345 346
	
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
347
	linphone_carddav_context_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
348 349 350 351
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
352
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
353
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
354
	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
355 356
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
357 358 359 360
	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");
361
	linphone_core_add_friend_list(manager->lc, lfl);
362
	linphone_friend_list_unref(lfl);
363 364
	c = linphone_carddav_context_new(lfl);
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
365 366 367
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
368
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382
	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
383 384 385 386

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
387
	linphone_carddav_context_destroy(c);
388 389 390 391
	linphone_core_manager_destroy(manager);
}

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

439 440 441 442 443
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);
	stats->updated_contact_count++;
}

444
static void carddav_integration(void) {
445
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
446 447 448
	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);
449 450
	LinphoneVCard *lvc2 = NULL;
	LinphoneFriend *lf2 = NULL;
451 452
	LinphoneFriendListCbs *cbs = NULL;
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
453
	const char *refkey = "toto";
454 455 456 457 458 459
	
	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);
460
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
461
	linphone_core_add_friend_list(manager->lc, lfl);
462 463 464 465

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
	BC_ASSERT_TRUE(linphone_vcard_generate_unique_id(lvc));
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
466
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
467 468 469
	wait_for_until(manager->lc, NULL, NULL, 1, 2000);
	linphone_friend_list_remove_friend(lfl, lf);
	wait_for_until(manager->lc, NULL, NULL, 1, 2000);
470
	linphone_friend_unref(lf);
471
	lf = NULL;
472 473 474

	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);
475
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_import_friend(lfl, lf, FALSE), LinphoneFriendListOK, int, "%d");
476
	linphone_friend_unref(lf);
477
	
478
	lvc2 = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sberfini.linphone.org\r\nUID:1f08dd48-29ac-4097-8e48-8596d7776283\r\nEND:VCARD\r\n");
479 480
	linphone_vcard_set_url(lvc2, "/sabredav/addressbookserver.php/addressbooks/sylvain/default/me.vcf");
	lf2 = linphone_friend_new_from_vcard(lvc2);
481
	linphone_friend_set_ref_key(lf2, refkey);
482
	BC_ASSERT_EQUAL_FATAL(linphone_friend_list_import_friend(lfl, lf2, FALSE), LinphoneFriendListOK, int, "%d");
483 484
	linphone_friend_unref(lf2);
	
485 486
	BC_ASSERT_EQUAL(lfl->revision, 0, int, "%i");
	linphone_friend_list_synchronize_friends_from_server(lfl);
487 488
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 0, 2000);
	BC_ASSERT_EQUAL(stats->new_contact_count, 0, int, "%i");
489 490
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
491 492
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 2000);
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
493 494
	BC_ASSERT_NOT_EQUAL(lfl->revision, 0, int, "%i");
	
495 496
	BC_ASSERT_EQUAL_FATAL(ms_list_size(lfl->friends), 1, int, "%i");
	lf = (LinphoneFriend *)lfl->friends->data;
497 498 499 500
	BC_ASSERT_STRING_EQUAL(lf->refkey, refkey);
	BC_ASSERT_EQUAL(lf->storage_id, lf2->storage_id, int, "%i");
	BC_ASSERT_STRING_EQUAL(linphone_address_as_string_uri_only(lf->uri), "sip:sylvain@sip.linphone.org");
	
501 502 503 504 505 506 507 508 509 510
	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");
	
511 512
	ms_free(stats);
	linphone_friend_list_unref(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
513 514 515
	linphone_core_manager_destroy(manager);
}

516 517 518
#else
static void dummy_test(void) {
}
519
#endif
520

521
test_t vcard_tests[] = {
522
#ifdef VCARD_ENABLED
Sylvain Berfini's avatar
Sylvain Berfini committed
523
	{ "Import / Export friends from vCards", linphone_vcard_import_export_friends_test },
524
	{ "Import a lot of friends from vCards", linphone_vcard_import_a_lot_of_friends_test },
525 526 527 528
#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 },
529
#endif
530
	{ "CardDAV synchronization", carddav_sync },
Sylvain Berfini's avatar
Sylvain Berfini committed
531 532
	{ "CardDAV synchronization 2", carddav_sync_2 },
	{ "CardDAV synchronization 3", carddav_sync_3 },
533
	{ "CardDAV synchronization 4", carddav_sync_4 },
534
	{ "CardDAV integration", carddav_integration },
535 536
#else
	{ "Dummy test", dummy_test }
537
#endif
538 539 540 541 542 543 544
};

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