vcard_tester.c 36.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 27
#define CARDDAV_SERVER "http://dav.linphone.org/card.php/addressbooks/tester/default"

28
#ifdef VCARD_ENABLED
Sylvain Berfini's avatar
Sylvain Berfini committed
29 30 31 32 33
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) {
34
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
35 36
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
	const MSList *friends = linphone_friend_list_get_friends(lfl);
37
	char *import_filepath = bc_tester_res("vcards/vcards.vcf");
Sylvain Berfini's avatar
Sylvain Berfini committed
38
	char *export_filepath = create_filepath(bc_tester_get_writable_dir_prefix(), "export_vcards", "vcf");
39 40
	int count = 0;
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
41

42
	count = linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
43
	BC_ASSERT_EQUAL(count, 3, int, "%d");
44
	friends = linphone_friend_list_get_friends(lfl);
45
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
46

47
	linphone_friend_list_export_friends_as_vcard4_file(lfl, export_filepath);
48

49 50
	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
51
	BC_ASSERT_EQUAL(count, 3, int, "%d");
52
	friends = linphone_friend_list_get_friends(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
53
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
54
	linphone_friend_list_unref(lfl);
55

Sylvain Berfini's avatar
Sylvain Berfini committed
56
	remove(export_filepath);
57 58
	ms_free(import_filepath);
	ms_free(export_filepath);
Sylvain Berfini's avatar
Sylvain Berfini committed
59
	linphone_core_manager_destroy(manager);
60 61
}

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

	start = clock();
74
	linphone_friend_list_import_friends_from_vcard4_file(lfl, import_filepath);
75
	end = clock();
76

77
	friends = linphone_friend_list_get_friends(lfl);
78
	BC_ASSERT_EQUAL(ms_list_size(friends), 482, int, "%i"); // Thousand vcards contains 482 contacts with a SIP URI
79

80
	elapsed = (double)(end - start);
81
	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);
82

83
	lfl = linphone_core_create_friend_list(manager->lc);
84 85 86 87 88 89 90 91 92
	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);
93

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

100 101
	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
102

103 104
	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);
105

106
	linphone_friend_list_unref(lfl);
107

108
	ms_free(import_filepath);
109 110 111
	linphone_core_manager_destroy(manager);
}

112 113
static void linphone_vcard_update_existing_friends_test(void) {
	LinphoneFriend *lf = linphone_friend_new_with_addr("sip:oldfriend@sip.linphone.org");
114

115
	BC_ASSERT_PTR_NOT_NULL(lf);
116 117 118 119 120
	BC_ASSERT_PTR_NULL(linphone_friend_get_vcard(lf));

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

122 123
	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
124 125
	linphone_friend_unref(lf);
	lf = NULL;
126 127
}

Sylvain Berfini's avatar
Sylvain Berfini committed
128 129 130 131 132 133
static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
	LinphoneVcard *lvc = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sberfini@sip.linphone.org\r\nIMPP;TYPE=home:sip:sylvain@sip.linphone.org\r\nTEL;TYPE=work:0952636505\r\nEND:VCARD\r\n");
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
	MSList *sip_addresses = linphone_friend_get_addresses(lf);
	MSList *phone_numbers = linphone_friend_get_phone_numbers(lf);
	LinphoneAddress *addr = NULL;
134

Sylvain Berfini's avatar
Sylvain Berfini committed
135 136 137 138 139
	BC_ASSERT_EQUAL(ms_list_size(sip_addresses), 2, int, "%i");
	BC_ASSERT_EQUAL(ms_list_size(phone_numbers), 1, int, "%i");
	if (sip_addresses) ms_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
	if (phone_numbers) ms_list_free(phone_numbers);
	linphone_friend_unref(lf);
140

Sylvain Berfini's avatar
Sylvain Berfini committed
141 142 143 144
	lvc = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nTEL;TYPE=work:0952636505\r\nTEL:0476010203\r\nEND:VCARD\r\n");
	lf = linphone_friend_new_from_vcard(lvc);
	sip_addresses = linphone_friend_get_addresses(lf);
	phone_numbers = linphone_friend_get_phone_numbers(lf);
145

Sylvain Berfini's avatar
Sylvain Berfini committed
146 147 148 149
	BC_ASSERT_EQUAL(ms_list_size(sip_addresses), 0, int, "%i");
	BC_ASSERT_EQUAL(ms_list_size(phone_numbers), 2, int, "%i");
	if (sip_addresses) ms_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
	if (phone_numbers) ms_list_free(phone_numbers);
150

Sylvain Berfini's avatar
Sylvain Berfini committed
151 152 153 154 155 156
	addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	linphone_friend_add_address(lf, addr);
	linphone_address_unref(addr);
	sip_addresses = linphone_friend_get_addresses(lf);
	BC_ASSERT_EQUAL(ms_list_size(sip_addresses), 1, int, "%i");
	if (sip_addresses) ms_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
157

Sylvain Berfini's avatar
Sylvain Berfini committed
158 159 160 161
	linphone_friend_remove_phone_number(lf, "0952636505");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
	BC_ASSERT_EQUAL(ms_list_size(phone_numbers), 1, int, "%i");
	if (phone_numbers) ms_list_free(phone_numbers);
162

Sylvain Berfini's avatar
Sylvain Berfini committed
163 164 165 166
	linphone_friend_remove_phone_number(lf, "0476010203");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
	BC_ASSERT_EQUAL(ms_list_size(phone_numbers), 0, int, "%i");
	if (phone_numbers) ms_list_free(phone_numbers);
167

Sylvain Berfini's avatar
Sylvain Berfini committed
168 169 170 171 172 173
	addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	linphone_friend_remove_address(lf, addr);
	linphone_address_unref(addr);
	sip_addresses = linphone_friend_get_addresses(lf);
	BC_ASSERT_EQUAL(ms_list_size(sip_addresses), 0, int, "%i");
	if (sip_addresses) ms_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
174

Sylvain Berfini's avatar
Sylvain Berfini committed
175 176 177 178
	linphone_friend_add_phone_number(lf, "+33952636505");
	phone_numbers = linphone_friend_get_phone_numbers(lf);
	BC_ASSERT_EQUAL(ms_list_size(phone_numbers), 1, int, "%i");
	if (phone_numbers) ms_list_free(phone_numbers);
179

Sylvain Berfini's avatar
Sylvain Berfini committed
180 181 182 183 184
	linphone_friend_unref(lf);
	lf = NULL;
	lvc = NULL;
}

185 186
static void friends_if_no_db_set(void) {
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
187
	LinphoneFriend *lf = linphone_core_create_friend(manager->lc);
188 189
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	const MSList *friends = NULL;
190
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
191

192 193
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
194 195
	linphone_friend_list_add_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(lfl);
196
	BC_ASSERT_EQUAL(ms_list_size(friends), 1, int, "%d");
197

198 199 200
	linphone_friend_list_remove_friend(lfl, lf);
	linphone_friend_unref(lf);
	friends = linphone_friend_list_get_friends(lfl);
201
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
202

203
	linphone_friend_list_unref(lfl);
204
	linphone_address_unref(addr);
205 206 207 208 209
	linphone_core_manager_destroy(manager);
}

#ifdef FRIENDS_SQL_STORAGE_ENABLED
static void friends_migration(void) {
210
	LinphoneCoreManager* manager = linphone_core_manager_new2("friends_rc", FALSE);
211
	LpConfig *lpc = linphone_core_get_config(manager->lc);
212 213
	LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
	const MSList *friends = linphone_friend_list_get_friends(lfl);
214 215
	MSList *friends_from_db = NULL;
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
216 217
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
	BC_ASSERT_EQUAL(lp_config_get_int(lpc, "misc", "friends_migration_done", 0), 0, int, "%i");
218

219 220
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
221
	lfl = linphone_core_get_default_friend_list(manager->lc);
222
	friends = linphone_friend_list_get_friends(lfl);
223
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
224
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc, lfl);
225
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 3, int, "%d");
226
	BC_ASSERT_EQUAL(lp_config_get_int(lpc, "misc", "friends_migration_done", 0), 1, int, "%i");
227

228
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
229 230 231 232 233
	unlink(friends_db);
	ms_free(friends_db);
	linphone_core_manager_destroy(manager);
}

234 235 236 237 238 239 240
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);
241 242 243
	if (stats) {
		stats->new_list_count++;
	}
244 245 246 247
}

static void friend_list_removed_cb(LinphoneCore *lc, LinphoneFriendList *list) {
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)linphone_friend_list_get_user_data(list);
248 249 250
	if (stats) {
		stats->removed_list_count++;
	}
251 252
}

253
static void friends_sqlite_storage(void) {
254 255 256
	LinphoneCoreVTable *v_table = linphone_core_v_table_new();
	LinphoneCore* lc = NULL;
	LinphoneFriendList *lfl = NULL;
257
	LinphoneFriend *lf = NULL;
258
	LinphoneFriend *lf2 = NULL;
259
	LinphoneVcard *lvc = linphone_vcard_new();
260
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
261
	const MSList *friends = NULL;
262
	MSList *friends_from_db = NULL;
263
	MSList *friends_lists_from_db = NULL;
264
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
265
	LinphoneFriendListStats *stats = (LinphoneFriendListStats *)ms_new0(LinphoneFriendListStats, 1);
266

267 268 269
	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);
270
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
271 272
	lfl = linphone_core_create_friend_list(lc);
	linphone_friend_list_set_user_data(lfl, stats);
273
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
274

275
	unlink(friends_db);
276 277
	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));
278
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");
279

280 281
	linphone_vcard_set_etag(lvc, "\"123-456789\"");
	linphone_vcard_set_url(lvc, "http://dav.somewhere.fr/addressbook/me/someone.vcf");
282
	lf = linphone_friend_new_from_vcard(lvc);
283 284
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
285

286 287 288
	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");
289
	linphone_friend_list_unref(lfl);
290
	linphone_friend_list_set_display_name(lfl, "Test");
291
	BC_ASSERT_EQUAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%i");
292
	linphone_friend_unref(lf);
293
	BC_ASSERT_EQUAL(lfl->storage_id, 1, int, "%d");
294
	BC_ASSERT_EQUAL(lf->storage_id, 1, int, "%d");
295

296
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
297
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
298

299
	friends_lists_from_db = linphone_core_fetch_friends_lists_from_db(lc);
300
	BC_ASSERT_EQUAL(ms_list_size(friends_lists_from_db), 1, int, "%d");
301 302 303 304 305
	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);
306
	friends_lists_from_db = ms_list_free_with_data(friends_lists_from_db, (void (*)(void *))linphone_friend_list_unref);
307

308
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
309
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
310 311 312 313 314 315
	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");
316 317
	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)));
318 319 320 321 322 323
	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);
324
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
325 326 327 328
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
	if (ms_list_size(friends_from_db) < 1) {
		goto end;
	}
329
	lf2 = (LinphoneFriend *)friends_from_db->data;
330 331
	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);
332

333 334
	linphone_friend_list_remove_friend(lfl, lf);
	friends = linphone_friend_list_get_friends(linphone_core_get_default_friend_list(lc));
335
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
336
	friends_from_db = linphone_core_fetch_friends_from_db(lc, lfl);
337
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");
338

339 340 341
	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");
342 343

end:
344
	ms_free(stats);
345 346
	unlink(friends_db);
	ms_free(friends_db);
347
	linphone_address_unref(addr);
348
	linphone_core_destroy(lc);
349 350
}
#endif
351

352 353 354 355 356 357 358
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

359
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
360
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
361
	BC_ASSERT_TRUE(success);
Sylvain Berfini's avatar
Sylvain Berfini committed
362
	stats->sync_done_count++;
363 364
}

365 366
static void carddav_new_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
367
	BC_ASSERT_PTR_NOT_NULL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
368
	stats->new_contact_count++;
369 370 371 372
}

static void carddav_removed_contact(LinphoneCardDavContext *c, LinphoneFriend *lf) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
373
	BC_ASSERT_PTR_NOT_NULL(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
374
	stats->removed_contact_count++;
375 376
}

377
static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *new_lf, LinphoneFriend *old_lf) {
378
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
379 380
	BC_ASSERT_PTR_NOT_NULL(new_lf);
	BC_ASSERT_PTR_NOT_NULL(old_lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
381
	stats->updated_contact_count++;
382 383
}

384
static void carddav_sync(void) {
385
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
386
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
387 388
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
389

390
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
391
	linphone_core_add_friend_list(manager->lc, lfl);
392
	linphone_friend_list_unref(lfl);
393
	c = linphone_carddav_context_new(lfl);
394
	BC_ASSERT_PTR_NOT_NULL(c);
395

396
	linphone_carddav_set_user_data(c, stats);
397
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
398 399 400
	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);
401
	linphone_carddav_synchronize(c);
402

403
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
404
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
405
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
406
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
407

Sylvain Berfini's avatar
Sylvain Berfini committed
408
	ms_free(stats);
409
	linphone_carddav_context_destroy(c);
410 411
	linphone_core_manager_destroy(manager);
}
Sylvain Berfini's avatar
Sylvain Berfini committed
412 413

static void carddav_sync_2(void) {
414
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
415
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
416
	LinphoneFriend *lf = linphone_core_create_friend_with_address(manager->lc, "\"Sylvain\" <sip:sylvain@sip.linphone.org>");
Sylvain Berfini's avatar
Sylvain Berfini committed
417
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
418 419
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
420

421
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
422
	linphone_core_add_friend_list(manager->lc, lfl);
423
	linphone_friend_list_unref(lfl);
424
	c = linphone_carddav_context_new(lfl);
425
	BC_ASSERT_PTR_NOT_NULL(c);
426

Sylvain Berfini's avatar
Sylvain Berfini committed
427 428
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
429
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
430
	linphone_friend_unref(lf);
431

Sylvain Berfini's avatar
Sylvain Berfini committed
432 433 434 435 436
	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);
437

Sylvain Berfini's avatar
Sylvain Berfini committed
438
	linphone_carddav_synchronize(c);
439

440
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
441
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
442
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
443
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
444
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
445
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
446

Sylvain Berfini's avatar
Sylvain Berfini committed
447 448 449
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
450
	linphone_carddav_context_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
451 452 453 454
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
455
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
Sylvain Berfini's avatar
Sylvain Berfini committed
456
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
457
	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
458 459
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
460 461
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
462

463
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
464
	linphone_core_add_friend_list(manager->lc, lfl);
465
	linphone_friend_list_unref(lfl);
466
	c = linphone_carddav_context_new(lfl);
467
	BC_ASSERT_PTR_NOT_NULL(c);
468

Sylvain Berfini's avatar
Sylvain Berfini committed
469 470
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
471
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
Sylvain Berfini's avatar
Sylvain Berfini committed
472
	linphone_friend_unref(lf);
473

Sylvain Berfini's avatar
Sylvain Berfini committed
474 475 476 477 478
	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);
479

Sylvain Berfini's avatar
Sylvain Berfini committed
480
	linphone_carddav_synchronize(c);
481

482
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
483
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
484
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
Sylvain Berfini's avatar
Sylvain Berfini committed
485
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
486 487 488 489

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
490
	linphone_carddav_context_destroy(c);
491
	c = NULL;
492 493 494 495
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_4(void) {
496
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
497
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
498
	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");
499
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
500 501
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
	LinphoneCardDavContext *c = NULL;
502

503
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
504
	linphone_core_add_friend_list(manager->lc, lfl);
505
	linphone_friend_list_unref(lfl);
506
	c = linphone_carddav_context_new(lfl);
507
	BC_ASSERT_PTR_NOT_NULL(c);
508

509 510
	linphone_carddav_set_user_data(c, stats);
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
511 512 513
	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);
514 515

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
516
	linphone_carddav_put_vcard(c, lf);
517
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
518
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
519
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
520

521
	linphone_carddav_delete_vcard(c, lf);
522
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
523 524 525 526
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");

	linphone_friend_unref(lf);
	ms_free(stats);
527 528 529 530 531 532 533 534 535 536 537 538 539 540
	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++;
}

541 542
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);
543
	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)));
544 545 546
	stats->updated_contact_count++;
}

547 548 549 550 551 552 553 554 555
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++;
	}
}

556
static void carddav_integration(void) {
557
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
558
	LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc);
559
	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");
560
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
561
	LinphoneVcard *lvc2 = NULL;
562
	LinphoneFriend *lf2 = NULL;
563 564
	LinphoneFriendListCbs *cbs = NULL;
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
565
	const char *refkey = "toto";
566

567
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
568 569 570 571
	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);
572
	linphone_friend_list_cbs_set_contact_updated(cbs, carddav_contact_updated);
573
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
574
	linphone_core_add_friend_list(manager->lc, lfl);
575 576

	BC_ASSERT_PTR_NULL(linphone_vcard_get_uid(lvc));
577
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
578
	BC_ASSERT_EQUAL(linphone_friend_list_add_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
579
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 1, int, "%d");
580
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
581
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
582 583
	BC_ASSERT_EQUAL(ms_list_size(lfl->dirty_friends_to_update), 0, int, "%d");
	BC_ASSERT_PTR_NOT_NULL(linphone_vcard_get_uid(lvc));
584
	linphone_friend_list_remove_friend(lfl, lf);
585
	BC_ASSERT_EQUAL(ms_list_size(lfl->friends), 0, int, "%d");
586
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 2, 5000);
587
	BC_ASSERT_EQUAL(stats->sync_done_count, 2, int, "%i");
588
	linphone_friend_unref(lf);
589
	lf = NULL;
590 591 592

	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);
593
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d");
594
	linphone_friend_unref(lf);
595

596
	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");
597
	linphone_vcard_set_url(lvc2, "/card.php/addressbooks/tester/default/me.vcf");
598
	lf2 = linphone_friend_new_from_vcard(lvc2);
599
	linphone_friend_set_ref_key(lf2, refkey);
600
	BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf2), LinphoneFriendListOK, int, "%d");
601

602 603
	BC_ASSERT_EQUAL(lfl->revision, 0, int, "%i");
	linphone_friend_list_synchronize_friends_from_server(lfl);
604
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 0, 5000);
605
	BC_ASSERT_EQUAL(stats->new_contact_count, 0, int, "%i");
606
	wait_for_until(manager->lc, NULL, &stats->removed_contact_count, 1, 5000);
607
	BC_ASSERT_EQUAL(stats->removed_contact_count, 1, int, "%i");
608
	wait_for_until(manager->lc, NULL, &stats->updated_contact_count, 1, 5000);
609
	BC_ASSERT_EQUAL(stats->updated_contact_count, 1, int, "%i");
610
	BC_ASSERT_NOT_EQUAL(lfl->revision, 0, int, "%i");
611
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 3, 5000);
612
	BC_ASSERT_EQUAL(stats->sync_done_count, 3, int, "%i");
613

614
	BC_ASSERT_EQUAL(ms_list_size(lfl->friends), 1, int, "%i");
615
	lf = (LinphoneFriend *)lfl->friends->data;
616 617
	BC_ASSERT_STRING_EQUAL(lf->refkey, refkey);
	BC_ASSERT_EQUAL(lf->storage_id, lf2->storage_id, int, "%i");
618
	linphone_friend_unref(lf2);
619
	BC_ASSERT_STRING_EQUAL(linphone_address_as_string_uri_only(lf->uri), "sip:sylvain@sip.linphone.org");
620

621 622 623
	linphone_friend_edit(lf);
	linphone_friend_done(lf);
	BC_ASSERT_EQUAL(ms_list_size(lf->friend_list->dirty_friends_to_update), 0, int, "%i");
624

625 626 627 628 629
	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");
630

631 632
	ms_free(stats);
	linphone_friend_list_unref(lfl);
Sylvain Berfini's avatar
Sylvain Berfini committed
633 634 635
	linphone_core_manager_destroy(manager);
}

636 637 638 639 640 641
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;
642
	MSList *friends_iterator = NULL;
643
	LinphoneFriend *lf = NULL;
644
	LinphoneVcard *lvc = NULL;
645

646 647 648 649
	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);
650
	linphone_friend_list_cbs_set_sync_status_changed(cbs, carddav_sync_status_changed);
651
	linphone_core_add_friend_list(manager->lc, lfl);
652
	linphone_friend_list_set_uri(lfl, CARDDAV_SERVER);
653

654
	linphone_friend_list_synchronize_friends_from_server(lfl);
655
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
656 657
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
	stats->sync_done_count = 0;
658

659
	friends = ms_list_copy(lfl->friends);
660 661 662
	friends_iterator = friends;
	while (friends_iterator) {
		LinphoneFriend *lf = (LinphoneFriend *)friends_iterator->data;
663
		linphone_friend_list_remove_friend(lfl, lf);
664
		wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
665 666
		BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
		stats->sync_done_count = 0;
667
		stats->removed_contact_count = 0;
668
		friends_iterator = ms_list_next(friends_iterator);
669
	}
670
	ms_list_free(friends);
671

672
	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");
673
	linphone_vcard_set_url(lvc, "http://dav.linphone.org/card.php/addressbooks/tester/default/me.vcf");
674 675
	lf = linphone_friend_new_from_vcard(lvc);
	linphone_friend_list_add_friend(lfl, lf);
676
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 5000);
677
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");