From 860ee180a8194fec545dfa099df4bd8d8a706e8f Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 17 Dec 2015 17:32:18 +0100 Subject: [PATCH] Tests done for friends' database storage --- coreapi/friend.c | 14 +++++-- coreapi/private.h | 2 +- tester/vcard_tester.c | 95 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) diff --git a/coreapi/friend.c b/coreapi/friend.c index 823b7fce8..0d14b73b6 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -960,7 +960,7 @@ void linphone_core_friends_storage_init(LinphoneCore *lc) { linphone_core_friends_storage_close(lc); - ret=_linphone_sqlite3_open(lc->friends_db_file, &db); + ret = _linphone_sqlite3_open(lc->friends_db_file, &db); if (ret != SQLITE_OK) { errmsg = sqlite3_errmsg(db); ms_error("Error in the opening: %s.\n", errmsg); @@ -986,8 +986,13 @@ void linphone_core_friends_storage_close(LinphoneCore *lc) { static int create_friend(void *data, int argc, char **argv, char **colName) { MSList **list = (MSList **)data; LinphoneFriend *lf = NULL; + LinphoneVCard *vcard = NULL; + + vcard = linphone_vcard_new_from_vcard4_buffer(argv[1]); + lf = linphone_friend_new_from_vcard(vcard); //TODO - *list = ms_list_append(*list, lf); + *list = ms_list_append(*list, linphone_friend_ref(lf)); + linphone_friend_unref(lf); return 0; } @@ -1047,7 +1052,7 @@ void linphone_core_remove_friend_from_db(LinphoneCore *lc, LinphoneFriend *lf) { } } -const MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc) { +MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc) { char *buf; uint64_t begin,end; MSList *result = NULL; @@ -1079,7 +1084,7 @@ void linphone_core_store_friend_in_db(LinphoneCore *lc, LinphoneFriend *lf) { void linphone_core_remove_friend_from_db(LinphoneCore *lc, LinphoneFriend *lf) { } -const MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc) { +MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc) { return NULL; } @@ -1103,4 +1108,5 @@ void linphone_core_migrate_friends_from_rc_to_db(LinphoneCore *lc) { ms_warning("linphone has been compiled without sqlite, can't migrate friends"); return; #endif + //TODO } \ No newline at end of file diff --git a/coreapi/private.h b/coreapi/private.h index 009adb05b..22c83669d 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -399,7 +399,7 @@ void linphone_core_friends_storage_init(LinphoneCore *lc); void linphone_core_friends_storage_close(LinphoneCore *lc); void linphone_core_store_friend_in_db(LinphoneCore *lc, LinphoneFriend *lf); void linphone_core_remove_friend_from_db(LinphoneCore *lc, LinphoneFriend *lf); -const MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc); +MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc); int parse_hostname_to_addr(const char *server, struct sockaddr_storage *ss, socklen_t *socklen, int default_port); diff --git a/tester/vcard_tester.c b/tester/vcard_tester.c index a7294c1c4..669868d3e 100644 --- a/tester/vcard_tester.c +++ b/tester/vcard_tester.c @@ -72,9 +72,104 @@ static void linphone_vcard_import_a_lot_of_friends_test(void) { linphone_core_manager_destroy(manager); } +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) { + 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"); + + 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"); + + 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_FATAL(ms_list_size(friends_from_db), 1, int, "%d"); + + lf2 = (LinphoneFriend *)friends_from_db->data; + BC_ASSERT_EQUAL(linphone_friend_get_name(lf2), linphone_friend_get_name(lf), const char *, "%s"); + + 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); + + friends = linphone_core_get_friend_list(manager->lc); + BC_ASSERT_EQUAL(ms_list_size(friends), 1, int, "%d"); + friends_from_db = linphone_core_fetch_friends_from_db(manager->lc); + BC_ASSERT_EQUAL(ms_list_size(friends_from_db), 1, int, "%d"); + + lf2 = (LinphoneFriend *)friends_from_db->data; + BC_ASSERT_EQUAL(linphone_friend_get_name(lf2), linphone_friend_get_name(lf), const char *, "%s"); + + 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 + test_t vcard_tests[] = { { "Import / Export friends from vCards", linphone_vcard_import_export_friends_test }, { "Import a lot of friends from vCards", linphone_vcard_import_a_lot_of_friends_test }, +#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 }, +#endif }; test_suite_t vcard_test_suite = {