From 43cfa382f69fadf4d85fcd7b358b120d2f5a0203 Mon Sep 17 00:00:00 2001
From: Andrea Gianarda <andrea.gianarda@belledonne-communications.com>
Date: Mon, 28 Aug 2023 15:33:46 +0200
Subject: [PATCH] Check that registration state goes through Cleared state when
 the core is destroyed. Fix return value of Account::customContactChanged when
 two addresses are compared

---
 coreapi/callbacks.c      |  2 +-
 src/account/account.cpp  | 10 +++++-----
 tester/register_tester.c |  3 +++
 tester/tester.c          |  3 +++
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c
index 0017d786ce..da1dfdc84f 100644
--- a/coreapi/callbacks.c
+++ b/coreapi/callbacks.c
@@ -513,7 +513,7 @@ static void register_success(SalOp *op, bool_t registered) {
 	}
 
 	// If this register is a refresh sent by belle-sip, then move to the Refreshing register first
-	if (Account::toCpp(account)->getPreviousState() == LinphoneRegistrationOk) {
+	if (registered && Account::toCpp(account)->getPreviousState() == LinphoneRegistrationOk) {
 		Account::toCpp(account)->setState(LinphoneRegistrationRefreshing, "Registration refreshing");
 	}
 
diff --git a/src/account/account.cpp b/src/account/account.cpp
index 25769f680d..db2d6f1af3 100644
--- a/src/account/account.cpp
+++ b/src/account/account.cpp
@@ -199,7 +199,7 @@ bool Account::customContactChanged() {
 	if (mParams->mCustomContact == nullptr && mOldParams->mCustomContact == nullptr) return false;
 	if (mParams->mCustomContact != nullptr && mOldParams->mCustomContact == nullptr) return true;
 	if (mParams->mCustomContact == nullptr && mOldParams->mCustomContact != nullptr) return true;
-	return !((*mOldParams->mCustomContact) != (*mParams->mCustomContact));
+	return ((*mOldParams->mCustomContact) != (*mParams->mCustomContact));
 }
 
 void Account::applyParamsChanges() {
@@ -349,7 +349,7 @@ void Account::setState(LinphoneRegistrationState state, const std::string &messa
 	auto core = getCCore();
 	if (mState != state ||
 	    state == LinphoneRegistrationOk) { /*allow multiple notification of LinphoneRegistrationOk for refreshing*/
-		const auto identity = (mParams) ? mParams->getIdentity().c_str() : std::string();
+		const auto identity = (mParams) ? mParams->getIdentityAddress()->toString() : std::string();
 		if (!mParams) lWarning() << "AccountParams not set for Account [" << this->toC() << "]";
 		lInfo() << "Account [" << this << "] for identity [" << identity << "] moving from state ["
 		        << linphone_registration_state_to_string(mState) << "] to ["
@@ -618,7 +618,6 @@ std::list<SalAddress *> Account::getOtherContacts() {
 
 void Account::registerAccount() {
 	if (mParams->mRegisterEnabled) {
-
 		std::shared_ptr<Address> proxy = Address::create(mParams->mProxy);
 		if (proxy == nullptr) {
 			lError() << "Can't register LinphoneAccount [" << this << "] without a proxy";
@@ -650,6 +649,7 @@ void Account::registerAccount() {
 		} else {
 			setState(LinphoneRegistrationFailed, "Registration failed");
 		}
+
 		for (auto ct : otherContacts)
 			sal_address_unref(ct);
 	} else {
@@ -1012,8 +1012,8 @@ void Account::apply(LinphoneCore *lc) {
 	if (mDependency != nullptr) {
 		// disable register if master account is not yet registered
 		if (mDependency->mState != LinphoneRegistrationOk) {
-			if (mParams->mRegisterEnabled != FALSE) {
-				mRegisterChanged = TRUE;
+			if (mParams->mRegisterEnabled != false) {
+				mRegisterChanged = true;
 			}
 			// We do not call enableRegister on purpose here
 			// Explicitely disabling register on a dependent config puts it in a disabled state (see
diff --git a/tester/register_tester.c b/tester/register_tester.c
index 57647a3069..6d65fe908b 100644
--- a/tester/register_tester.c
+++ b/tester/register_tester.c
@@ -268,6 +268,9 @@ static void register_with_custom_headers(void) {
 	value = linphone_proxy_config_get_custom_header(cfg, "Server");
 	BC_ASSERT_PTR_NOT_NULL(value);
 	if (value) BC_ASSERT_PTR_NOT_NULL(strstr(value, "Flexisip"));
+
+	linphone_core_clear_accounts(marie->lc);
+	wait_for(marie->lc, NULL, &marie->stat.number_of_LinphoneRegistrationCleared, 1);
 	linphone_core_manager_destroy(marie);
 }
 
diff --git a/tester/tester.c b/tester/tester.c
index 40a14661a6..54271a3fec 100644
--- a/tester/tester.c
+++ b/tester/tester.c
@@ -2749,6 +2749,7 @@ static void check_orphan_nat_policy_section(LinphoneCoreManager *mgr) {
 
 void linphone_core_manager_stop(LinphoneCoreManager *mgr) {
 	if (mgr->lc) {
+		int previousNbRegistrationCleared = mgr->stat.number_of_LinphoneRegistrationCleared;
 		const char *record_file = linphone_core_get_record_file(mgr->lc);
 		if (!liblinphone_tester_keep_record_files && record_file && bctbx_file_exist(record_file) == 0) {
 			if ((bc_get_number_of_failures() - mgr->number_of_bcunit_error_at_creation) > 0) {
@@ -2759,6 +2760,8 @@ void linphone_core_manager_stop(LinphoneCoreManager *mgr) {
 		}
 
 		linphone_core_stop(mgr->lc);
+		wait_for_until(mgr->lc, NULL, &mgr->stat.number_of_LinphoneRegistrationCleared,
+		               previousNbRegistrationCleared + 1, 2000);
 		check_orphan_nat_policy_section(mgr);
 		linphone_core_unref(mgr->lc);
 		mgr->lc = NULL;
-- 
GitLab