From 899730a6881a106561117c2938f5ded55a59ff40 Mon Sep 17 00:00:00 2001 From: Rangaiah Date: Fri, 17 Jan 2025 13:43:36 +0530 Subject: [PATCH] Add unit tests for webconfig poke in progress primary and supplementary retry sync --- .github/workflows/push.yml | 2 +- src/webcfg_multipart.c | 6 ++- src/webcfg_multipart.h | 1 + src/webcfg_rbus.c | 5 ++ src/webcfg_rbus.h | 1 + tests/test_multipart_unittest.c | 11 +++++ tests/test_rbus_fr.c | 83 +++++++++++++++++++++++++++++++-- 7 files changed, 103 insertions(+), 6 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 48e42055..248f04ff 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -31,7 +31,7 @@ jobs: - name: Install packages run: | sudo apt update - sudo apt-get -y install valgrind libcunit1 libcunit1-doc libcunit1-dev libmsgpack-dev gcovr libtool xsltproc docbook-xsl + sudo apt-get -y install valgrind libcunit1 libcunit1-doc libcunit1-dev libmsgpack-dev gcovr libtool xsltproc docbook-xsl uuid-dev pip install codecov - name: Make Build Directory diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 1c9f528f..32556889 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -1793,7 +1793,7 @@ void createCurlHeader( struct curl_slist *list, struct curl_slist **header_list, WebcfgDebug("Failed to get systemReadyTime\n"); } - if((g_ForceSyncTransID !=NULL) && (strlen(g_ForceSyncTransID)>0)) + if(strlen(g_ForceSyncTransID)>0) { WebcfgInfo("updating transaction_uuid with force g_ForceSyncTransID\n"); transaction_uuid = strdup(g_ForceSyncTransID); @@ -2586,3 +2586,7 @@ void setForceSyncTransID(char *ForceSyncTransID) webcfgStrncpy( g_ForceSyncTransID, ForceSyncTransID, sizeof(g_ForceSyncTransID)); } } + +const char* getForceSyncTransID() { + return g_ForceSyncTransID; +} diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index de6ce2f9..4f63c468 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -83,6 +83,7 @@ size_t headr_callback(char *buffer, size_t size, size_t nitems, void* data); size_t writer_callback_fn(void *buffer, size_t size, size_t nmemb, void *datain); WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id); void setForceSyncTransID(char *ForceSyncTransID); +const char* getForceSyncTransID(); char* generate_trans_uuid(); #ifdef WAN_FAILOVER_SUPPORTED void set_global_interface(char * value); diff --git a/src/webcfg_rbus.c b/src/webcfg_rbus.c index 882a25cd..2b539f15 100755 --- a/src/webcfg_rbus.c +++ b/src/webcfg_rbus.c @@ -2461,3 +2461,8 @@ void set_global_supplementary_url(char *value) WEBCFG_FREE(SupplementaryURLVal); SupplementaryURLVal = strdup(value); } + +ForceSyncMsg* getForceSyncMsgQueue() +{ + return ForceSyncMsgQ; +} diff --git a/src/webcfg_rbus.h b/src/webcfg_rbus.h index e8cd765d..4d268c87 100755 --- a/src/webcfg_rbus.h +++ b/src/webcfg_rbus.h @@ -150,4 +150,5 @@ int addForceSyncMsgToQueue(char *ForceSync, char *ForceSyncTransID); int updateForceSyncMsgQueue(char* trans_id); void deleteForceSyncMsgQueue(); void DisplayQueue(); +ForceSyncMsg* getForceSyncMsgQueue(); #endif diff --git a/tests/test_multipart_unittest.c b/tests/test_multipart_unittest.c index f72add81..24e2a0e1 100644 --- a/tests/test_multipart_unittest.c +++ b/tests/test_multipart_unittest.c @@ -1100,6 +1100,16 @@ void test_processMsgpackSubdoc_failure() CU_ASSERT_EQUAL(result, WEBCFG_FAILURE); } +void test_setForceTransID() { + // Test with a valid transaction ID + char testTransID[] = "1234"; + setForceSyncTransID(testTransID); + CU_ASSERT_STRING_EQUAL(getForceSyncTransID(), "1234"); + // Test with a NULL transaction ID + setForceSyncTransID(NULL); + CU_ASSERT_STRING_EQUAL(getForceSyncTransID(), ""); // g_ForceSyncTransID should be cleared +} + #ifdef WEBCONFIG_BIN_SUPPORT void test_processMsgpackSubdoc_msgpack_failure() { @@ -1279,6 +1289,7 @@ void add_suites( CU_pSuite *suite ) CU_add_test( *suite, "test readFromFile_success", test_readFromFile_success); CU_add_test( *suite, "test readFromFile_failure", test_readFromFile_failure); CU_add_test( *suite, "test processMsgpackSubdoc_failure", test_processMsgpackSubdoc_failure); + CU_add_test( *suite, "test reset_global_eventFlag", test_setForceTransID); #ifdef WEBCONFIG_BIN_SUPPORT CU_add_test( *suite, "test processMsgpackSubdoc_msgpack_failure", test_processMsgpackSubdoc_msgpack_failure); CU_add_test( *suite, "test processMsgpackSubdoc_setValues_rbus", test_processMsgpackSubdoc_setValues_rbus); diff --git a/tests/test_rbus_fr.c b/tests/test_rbus_fr.c index f8f7cf15..9be8f0b4 100644 --- a/tests/test_rbus_fr.c +++ b/tests/test_rbus_fr.c @@ -181,6 +181,51 @@ void set_global_supplementarySync(int value) /* Tests */ /*----------------------------------------------------------------------------*/ +void test_addForceSyncMsgToQueue(void) +{ + int ret = -1; + ret = addForceSyncMsgToQueue("root", "TID1"); + CU_ASSERT_EQUAL(ret, 0); + deleteForceSyncMsgQueue(); +} + +void test_DisplayQueue() +{ + addForceSyncMsgToQueue("ForceSync1", "TransID1"); + DisplayQueue(); + deleteForceSyncMsgQueue(); +} + +void test_deleteForceSyncMsgQueue() +{ + CU_ASSERT_EQUAL(addForceSyncMsgToQueue("ForceSync1", "TransID1"), 0); + CU_ASSERT_PTR_NOT_NULL(getForceSyncMsgQueue()); + deleteForceSyncMsgQueue(); + CU_ASSERT_PTR_NULL(getForceSyncMsgQueue()); +} + +void test_updateForceSyncMsgQueue_Failure() +{ + char *trans_id = NULL; + int result = updateForceSyncMsgQueue(trans_id); + CU_ASSERT_EQUAL(result, 0); +} + +void test_updateForceSyncMsgQueue_found(void) { + // Initialize the ForceSyncMsg queue with two same doc entrys and different trasaction ids + char* pString1 = "{ \"value\":\"telemetry\", \"transaction_id\":\"12345\"}"; + char* pString2 = "{ \"value\":\"telemetry\", \"transaction_id\":\"54321\"}"; + int session_status = 0; + set_rbus_ForceSync(pString1,&session_status); + set_rbus_ForceSync(pString2,&session_status); + + char *str = NULL; + char* transID = NULL; + int retGet = get_rbus_ForceSync(&str, &transID); + CU_ASSERT_EQUAL(retGet,1); + CU_ASSERT_STRING_EQUAL(transID,"54321"); //Latest trasation id should receive +} + // Test cases for set_rbus_ForceSync & get_rbus_ForceSync void test_setForceSync() { @@ -188,7 +233,7 @@ void test_setForceSync() int ret = set_rbus_ForceSync("root", &session_status); CU_ASSERT_EQUAL(0,session_status); CU_ASSERT_EQUAL(1,ret); - + get_webcfgReady_flag = false; ret = set_rbus_ForceSync("root", &session_status); CU_ASSERT_EQUAL(2,session_status); @@ -212,12 +257,37 @@ void test_setForceSync() CU_ASSERT_EQUAL(1,session_status); CU_ASSERT_EQUAL(0,ret); get_global_webcfg_forcedsync_started_flag = false; - - get_cloud_forcesync_retry_started_flag = true; + + get_cloud_forcesync_retry_started_flag = true; ret = set_rbus_ForceSync("root", &session_status); CU_ASSERT_EQUAL(1,session_status); CU_ASSERT_EQUAL(0,ret); - get_cloud_forcesync_retry_started_flag = false; + get_cloud_forcesync_retry_started_flag = false; + + ret = set_rbus_ForceSync("root,telemetry", &session_status); + CU_ASSERT_EQUAL(1,session_status); + CU_ASSERT_EQUAL(0,ret); + ret = set_rbus_ForceSync("telemetry,root", &session_status); + CU_ASSERT_EQUAL(1,session_status); + CU_ASSERT_EQUAL(0,ret); + + set_rbus_ForceSync("root", &session_status); + ret = set_rbus_ForceSync("telemetry,root", &session_status); + CU_ASSERT_EQUAL(1,session_status); + CU_ASSERT_EQUAL(0,ret); + ForceSyncMsg* head = getForceSyncMsgQueue(); + CU_ASSERT_PTR_NOT_NULL(head); // Ensure queue is not null + // Check the first node contains "telemetry" + CU_ASSERT_PTR_NOT_NULL(head->ForceSyncVal); + CU_ASSERT_STRING_EQUAL(head->ForceSyncVal, "telemetry"); + // Move to the next node and check it contains "root" + ForceSyncMsg* second = head->next; + CU_ASSERT_PTR_NOT_NULL(second); // Ensure the second node exists + CU_ASSERT_PTR_NOT_NULL(second->ForceSyncVal); + CU_ASSERT_STRING_EQUAL(second->ForceSyncVal, "root"); + // Ensure there are no more nodes in the queue + CU_ASSERT_PTR_NULL(second->next); + deleteForceSyncMsgQueue(); } void test_setForceSync_failure() @@ -1866,6 +1936,11 @@ void add_suites( CU_pSuite *suite ) CU_add_test( *suite, "test rbusWebcfgEventHandler", test_rbusWebcfgEventHandler); CU_add_test( *suite, "test fetchMpBlobData", test_fetchMpBlobData); CU_add_test( *suite, "test webcfg_util_method", test_webcfg_util_method); + CU_add_test( *suite, "test addForceSyncMsgToQueue", test_addForceSyncMsgToQueue); + CU_add_test( *suite, "test DisplayQueue", test_DisplayQueue); + CU_add_test( *suite, "test deleteForceSyncMsgQueue_simple", test_deleteForceSyncMsgQueue); + CU_add_test( *suite, "test updateForceSyncMsgQueue", test_updateForceSyncMsgQueue_Failure); + CU_add_test( *suite, "test DisplayQueue", test_updateForceSyncMsgQueue_found); #ifdef WAN_FAILOVER_SUPPORTED CU_add_test( *suite, "test eventReceiveHandler", test_eventReceiveHandler); CU_add_test( *suite, "test subscribeTo_CurrentActiveInterface_Event", test_subscribeTo_CurrentActiveInterface_Event);