From 1c80e72cb154b8202a1e13c57063749fabffb61d Mon Sep 17 00:00:00 2001 From: Benjamin Reis Date: Mon, 26 Jun 2017 11:08:38 +0200 Subject: [PATCH] Recreate ZRTP DB file when corrupted and test --- coreapi/linphonecore.c | 7 +++ tester/call_single_tester.c | 92 ++++++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 967979acd..86894d3b1 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -6595,6 +6595,9 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) { #ifdef SQLITE_STORAGE_ENABLED int ret; const char *errmsg; + const char *backupExtension = "_backup"; + char *backupName = malloc(snprintf(NULL, 0, "%s%s", fileName, backupExtension) + 1); + sprintf(backupName, "%s%s", fileName, backupExtension); sqlite3 *db; linphone_core_zrtp_cache_close(lc); @@ -6604,6 +6607,8 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) { errmsg = sqlite3_errmsg(db); ms_error("Error in the opening zrtp_cache_db_file(%s): %s.\n", fileName, errmsg); sqlite3_close(db); + unlink(backupName); + rename(fileName, backupName); lc->zrtp_cache_db=NULL; return; } @@ -6617,6 +6622,8 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) { } else if(ret != 0) { /* something went wrong */ ms_error("Zrtp cache failed to initialise(returned -%x), run cacheless", -ret); sqlite3_close(db); + unlink(backupName); + rename(fileName, backupName); lc->zrtp_cache_db = NULL; return; } diff --git a/tester/call_single_tester.c b/tester/call_single_tester.c index 60e781bcd..879babe60 100644 --- a/tester/call_single_tester.c +++ b/tester/call_single_tester.c @@ -6032,6 +6032,95 @@ static void call_with_network_reachable_down_in_callback(void){ linphone_core_manager_destroy(marie); } +static void recreate_zrtpdb_when_corrupted(void) { +#ifdef SQLITE_STORAGE_ENABLED + LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc"); + LinphoneCoreManager* pauline = linphone_core_manager_new("pauline_tcp_rc"); + + if (BC_ASSERT_TRUE(linphone_core_media_encryption_supported(marie->lc,LinphoneMediaEncryptionZRTP))) { + void *db; + const char* db_file; + const char *filepath; + const char *filepath2; + const char *corrupt = "corrupt mwahahahaha"; + FILE *f; + + remove(bc_tester_file("tmpZIDCacheMarie.sqlite")); + filepath = bc_tester_file("tmpZIDCacheMarie.sqlite"); + remove(bc_tester_file("tmpZIDCachePauline.sqlite")); + filepath2 = bc_tester_file("tmpZIDCachePauline.sqlite"); + linphone_core_set_media_encryption(marie->lc,LinphoneMediaEncryptionZRTP); + linphone_core_set_media_encryption(pauline->lc,LinphoneMediaEncryptionZRTP); + linphone_core_set_zrtp_secrets_file(marie->lc, filepath); + linphone_core_set_zrtp_secrets_file(pauline->lc, filepath2); + + BC_ASSERT_TRUE(call(pauline,marie)); + linphone_call_set_authentication_token_verified(linphone_core_get_current_call(marie->lc), TRUE); + linphone_call_set_authentication_token_verified(linphone_core_get_current_call(pauline->lc), TRUE); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(marie->lc))); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(pauline->lc))); + end_call(marie, pauline); + + db = linphone_core_get_zrtp_cache_db(marie->lc); + BC_ASSERT_PTR_NOT_NULL(db); + + BC_ASSERT_TRUE(call(pauline,marie)); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(marie->lc))); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(pauline->lc))); + end_call(marie, pauline); + + //Corrupt db file + db_file = linphone_core_get_zrtp_secrets_file(marie->lc); + BC_ASSERT_PTR_NOT_NULL(db_file); + + f = fopen(db_file, "wb"); + fwrite(corrupt, 1, sizeof(corrupt), f); + fclose(f); + + //Simulate relaunch of linphone core marie + linphone_core_set_zrtp_secrets_file(marie->lc, filepath); + db = linphone_core_get_zrtp_cache_db(marie->lc); + BC_ASSERT_PTR_NULL(db); + + BC_ASSERT_TRUE(call(pauline,marie)); + linphone_call_set_authentication_token_verified(linphone_core_get_current_call(marie->lc), TRUE); + linphone_call_set_authentication_token_verified(linphone_core_get_current_call(pauline->lc), TRUE); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(marie->lc))); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(pauline->lc))); + end_call(marie, pauline); + + BC_ASSERT_TRUE(call(pauline,marie)); + BC_ASSERT_FALSE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(marie->lc))); + BC_ASSERT_FALSE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(pauline->lc))); + end_call(marie, pauline); + + //Db file should be recreated after corruption + //Simulate relaunch of linphone core marie + linphone_core_set_zrtp_secrets_file(marie->lc, filepath); + + BC_ASSERT_TRUE(call(pauline,marie)); + linphone_call_set_authentication_token_verified(linphone_core_get_current_call(marie->lc), TRUE); + linphone_call_set_authentication_token_verified(linphone_core_get_current_call(pauline->lc), TRUE); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(marie->lc))); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(pauline->lc))); + end_call(marie, pauline); + + db = linphone_core_get_zrtp_cache_db(marie->lc); + BC_ASSERT_PTR_NOT_NULL(db); + db_file = linphone_core_get_zrtp_secrets_file(marie->lc); + BC_ASSERT_PTR_NOT_NULL(db_file); + + BC_ASSERT_TRUE(call(pauline,marie)); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(marie->lc))); + BC_ASSERT_TRUE(linphone_call_get_authentication_token_verified(linphone_core_get_current_call(pauline->lc))); + end_call(marie, pauline); + } + + linphone_core_manager_destroy(marie); + linphone_core_manager_destroy(pauline); +#endif /* SQLITE_STORAGE_ENABLED */ +} + test_t call_tests[] = { TEST_NO_TAG("Early declined call", early_declined_call), TEST_NO_TAG("Call declined", call_declined), @@ -6182,7 +6271,8 @@ test_t call_tests[] = { TEST_NO_TAG("Call terminated with reason", terminate_call_with_error), TEST_NO_TAG("Call cancelled with reason", cancel_call_with_error), TEST_NO_TAG("Call accepted, other ringing device receive CANCEL with reason", cancel_other_device_after_accept), - TEST_NO_TAG("Call declined, other ringing device receive CANCEL with reason", cancel_other_device_after_decline) + TEST_NO_TAG("Call declined, other ringing device receive CANCEL with reason", cancel_other_device_after_decline), + TEST_NO_TAG("Recreate ZRTP db file when corrupted", recreate_zrtpdb_when_corrupted) }; test_suite_t call_test_suite = {"Single Call", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,