vcard_tester.c 14.3 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 37 38
	const MSList *friends = linphone_core_get_friend_list(manager->lc);
	int count = 0;
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
Sylvain Berfini's avatar
Sylvain Berfini committed
39
	count = linphone_core_import_friends_from_vcard4_file(manager->lc, import_filepath);
40 41 42 43
	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
44
	linphone_core_export_friends_as_vcard4_file(manager->lc, export_filepath);
45
	
46
	linphone_core_set_friend_list(manager->lc, NULL);
Sylvain Berfini's avatar
Sylvain Berfini committed
47 48
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
49
	
Sylvain Berfini's avatar
Sylvain Berfini committed
50 51 52 53 54 55 56
	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);
	linphone_core_manager_destroy(manager);
57 58
}

59 60 61 62 63
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;
64
	const MSList *friends = NULL;
65 66 67 68 69

	start = clock();
	linphone_core_import_friends_from_vcard4_file(manager->lc, import_filepath);
	end = clock();
	
70
	friends = linphone_core_get_friend_list(manager->lc);
71
	elapsed = (double)(end - start);
72
	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);
73
	BC_ASSERT_TRUE(elapsed < 1500000); // 1.5 seconds
74 75 76
	linphone_core_manager_destroy(manager);
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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");
	
	linphone_address_destroy(addr);
	linphone_core_manager_destroy(manager);
}

#ifdef FRIENDS_SQL_STORAGE_ENABLED
static void friends_migration(void) {
100
	LinphoneCoreManager* manager = linphone_core_manager_new2("friends_rc", FALSE);
101 102 103 104 105 106 107 108
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	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);
109 110
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 3, int, "%d");
111
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc);
112 113 114 115
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 3, int, "%d");
	if (ms_list_size(friends_from_db) < 3) {
		goto end;
	}
116
	
117
end:
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	unlink(friends_db);
	ms_free(friends_db);
	linphone_address_destroy(addr);
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
	linphone_core_manager_destroy(manager);
}

static void friends_sqlite_storage(void) {
	LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE);
	LinphoneFriend *lf = linphone_friend_new();
	LinphoneFriend *lf2 = NULL;
	LinphoneAddress *addr = linphone_address_new("sip:sylvain@sip.linphone.org");
	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);
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 0, int, "%d");
	
	linphone_friend_set_address(lf, addr);
	linphone_friend_set_name(lf, "Sylvain");
	linphone_core_add_friend(manager->lc, lf);
	linphone_friend_unref(lf);
144
	BC_ASSERT_EQUAL(lf->storage_id, 1, int, "%d");
145 146 147
	
	friends = linphone_core_get_friend_list(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends), 1, int, "%d");
148
	
149 150
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	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");
	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);
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d");
	if (ms_list_size(friends_from_db) < 1) {
		goto end;
	}
168
	lf2 = (LinphoneFriend *)friends_from_db->data;
169 170
	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);
171
	
172 173 174 175 176 177 178
	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");
	friends_from_db = linphone_core_fetch_friends_from_db(manager->lc);
	BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 0, int, "%d");

end:
179 180 181 182 183 184 185
	unlink(friends_db);
	ms_free(friends_db);
	linphone_address_destroy(addr);
	friends_from_db = ms_list_free_with_data(friends_from_db, (void (*)(void *))linphone_friend_unref);
	linphone_core_manager_destroy(manager);
}
#endif
186

187 188 189 190 191 192 193
typedef struct _LinphoneCardDAVStats {
	int sync_done_count;
	int new_contact_count;
	int removed_contact_count;
	int updated_contact_count;
} LinphoneCardDAVStats;

194
static void carddav_sync_done(LinphoneCardDavContext *c, bool_t success, const char *message) {
195
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
196 197
	BC_ASSERT_TRUE(success);
	linphone_carddav_destroy(c);
Sylvain Berfini's avatar
Sylvain Berfini committed
198
	stats->sync_done_count++;
199 200
}

201 202 203 204
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);
	linphone_friend_unref(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
205
	stats->new_contact_count++;
206 207 208 209 210 211
}

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);
	linphone_friend_unref(lf);
Sylvain Berfini's avatar
Sylvain Berfini committed
212
	stats->removed_contact_count++;
213 214 215 216 217 218 219 220
}

static void carddav_updated_contact(LinphoneCardDavContext *c, LinphoneFriend *lf1, LinphoneFriend *lf2) {
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)linphone_carddav_get_user_data(c);
	BC_ASSERT_PTR_NOT_NULL_FATAL(lf1);
	BC_ASSERT_PTR_NOT_NULL_FATAL(lf2);
	linphone_friend_unref(lf1);
	linphone_friend_unref(lf2);
Sylvain Berfini's avatar
Sylvain Berfini committed
221
	stats->updated_contact_count++;
222 223
}

224 225 226
static void carddav_sync(void) {
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
	LinphoneCardDavContext *c = linphone_core_create_carddav_context(manager->lc);
227
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
228 229 230 231 232 233
	
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	BC_ASSERT_PTR_NOT_NULL(c->server_url);
	BC_ASSERT_PTR_NOT_NULL(c->username);
	BC_ASSERT_PTR_NOT_NULL(c->ha1);
	
234
	linphone_carddav_set_user_data(c, stats);
235
	linphone_carddav_set_synchronization_done_callback(c, carddav_sync_done);
236 237 238
	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);
239 240
	linphone_carddav_synchronize(c);
	
241
	wait_for_until(manager->lc, NULL, &stats->new_contact_count, 1, 2000);
Sylvain Berfini's avatar
Sylvain Berfini committed
242
	BC_ASSERT_EQUAL(stats->new_contact_count, 1, int, "%i");
243
	wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, 2000);
Sylvain Berfini's avatar
Sylvain Berfini committed
244
	BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i");
Sylvain Berfini's avatar
Sylvain Berfini committed
245 246
	
	ms_free(stats);
247 248
	linphone_core_manager_destroy(manager);
}
Sylvain Berfini's avatar
Sylvain Berfini committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

static void carddav_sync_2(void) {
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
	LinphoneCardDavContext *c = linphone_core_create_carddav_context(manager->lc);
	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");
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
	linphone_core_add_friend(manager->lc, lf);
	linphone_friend_unref(lf);
	
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	BC_ASSERT_PTR_NOT_NULL(c->server_url);
	BC_ASSERT_PTR_NOT_NULL(c->username);
	BC_ASSERT_PTR_NOT_NULL(c->ha1);
	
	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
281 282 283 284
	
	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
Sylvain Berfini's avatar
Sylvain Berfini committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	linphone_core_manager_destroy(manager);
}

static void carddav_sync_3(void) {
	LinphoneCoreManager *manager = linphone_core_manager_new2("carddav_rc", FALSE);
	LinphoneCardDavContext *c = linphone_core_create_carddav_context(manager->lc);
	LinphoneCardDAVStats *stats = (LinphoneCardDAVStats *)ms_new0(LinphoneCardDAVStats, 1);
	LinphoneVCard *lvc = linphone_vcard_new_from_vcard4_buffer("BEGIN:VCARD\r\nVERSION:4.0\r\nUID:79100a4d-2806-482f-bf27-0e09dc47149b\r\nFN:Sylvain Berfini\r\nIMPP;TYPE=work:sip:sylvain@sip.linphone.org\r\nEND:VCARD\r\n");
	LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc);
	char *friends_db = create_filepath(bc_tester_get_writable_dir_prefix(), "friends", "db");
	
	unlink(friends_db);
	linphone_core_set_friends_database_path(manager->lc, friends_db);
	linphone_core_add_friend(manager->lc, lf);
	linphone_friend_unref(lf);
	
	BC_ASSERT_PTR_NOT_NULL_FATAL(c);
	BC_ASSERT_PTR_NOT_NULL(c->server_url);
	BC_ASSERT_PTR_NOT_NULL(c->username);
	BC_ASSERT_PTR_NOT_NULL(c->ha1);
	
	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
318 319 320 321

	ms_free(stats);
	unlink(friends_db);
	ms_free(friends_db);
Sylvain Berfini's avatar
Sylvain Berfini committed
322 323 324
	linphone_core_manager_destroy(manager);
}

325 326 327 328
#else
static void dummy_test(void) {
	
}
329
#endif
330

331
test_t vcard_tests[] = {
332
#ifdef VCARD_ENABLED
Sylvain Berfini's avatar
Sylvain Berfini committed
333
	{ "Import / Export friends from vCards", linphone_vcard_import_export_friends_test },
334
	{ "Import a lot of friends from vCards", linphone_vcard_import_a_lot_of_friends_test },
335 336 337 338
#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 },
339
#endif
340
	{ "CardDAV synchronization", carddav_sync },
Sylvain Berfini's avatar
Sylvain Berfini committed
341 342
	{ "CardDAV synchronization 2", carddav_sync_2 },
	{ "CardDAV synchronization 3", carddav_sync_3 },
343 344
#else
	{ "Dummy test", dummy_test }
345
#endif
346 347 348 349 350 351 352
};

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