From bad3214fa3043e10c58162e88b5680b0a847a7d4 Mon Sep 17 00:00:00 2001 From: Rangaiah Date: Fri, 20 Oct 2023 14:33:24 +0530 Subject: [PATCH] Add unit tests for webcfg_notify.c --- src/webcfg_timer.h | 1 + tests/CMakeLists.txt | 29 +++- tests/test_notify.c | 179 ++++++++++++++++++++++++ tests/test_timer.c | 324 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 532 insertions(+), 1 deletion(-) create mode 100644 tests/test_notify.c create mode 100644 tests/test_timer.c diff --git a/src/webcfg_timer.h b/src/webcfg_timer.h index 6182df3f..c0367780 100644 --- a/src/webcfg_timer.h +++ b/src/webcfg_timer.h @@ -38,6 +38,7 @@ int getMaintenanceSyncSeconds(int maintenance_count); int retrySyncSeconds(); long getTimeInSeconds(long long time); void set_global_maintenance_time(long value); +long get_global_maintenance_time(); void set_global_retry_timestamp(long value); long get_global_retry_timestamp(); int updateRetryTimeDiff(long long expiry_time); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 73190f49..dd6a9c0a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -308,6 +308,28 @@ endif (WEBCONFIG_BIN_SUPPORT) target_link_libraries (test_generic gcov -Wl,--no-as-needed ) +#------------------------------------------------------------------------------- +# test_timer +#------------------------------------------------------------------------------- +add_test(NAME test_timer COMMAND ${MEMORY_CHECK} ./test_timer) +add_executable(test_timer test_timer.c ../src/webcfg_timer.c) +target_link_libraries (test_timer -lcunit -lmsgpackc -lcimplog -ltrower-base64 -lwdmp-c -lcjson) + +if (WEBCONFIG_BIN_SUPPORT) +target_link_libraries (test_timer -lrbus) +endif (WEBCONFIG_BIN_SUPPORT) + +target_link_libraries (test_timer gcov -Wl,--no-as-needed ) + +#------------------------------------------------------------------------------- +# test_notify +#------------------------------------------------------------------------------- +add_test(NAME test_notify COMMAND ${MEMORY_CHECK} ./test_notify) +add_executable(test_notify test_notify.c ../src/webcfg_notify.c) +target_link_libraries (test_notify -lcunit -lmsgpackc -lcimplog -ltrower-base64 -lwdmp-c -lcjson -lpthread) + +target_link_libraries (test_notify gcov -Wl,--no-as-needed ) + # Code coverage add_custom_target(coverage @@ -335,7 +357,10 @@ COMMAND lcov -q --capture --directory ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_generic.dir/__/src --output-file test_generic.info COMMAND lcov -q --capture --directory ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_rbus_fr.dir/__/src --output-file test_rbus_fr.info - +COMMAND lcov -q --capture --directory +${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_timer.dir/__/src --output-file test_timer.info +COMMAND lcov -q --capture --directory +${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_notify.dir/__/src --output-file test_notify.info COMMAND lcov -a test_events_supp.info @@ -350,6 +375,8 @@ COMMAND lcov -a test_webcfgpack.info -a test_generic.info -a test_rbus_fr.info +-a test_timer.info +-a test_notify.info --output-file coverage.info COMMAND genhtml coverage.info diff --git a/tests/test_notify.c b/tests/test_notify.c new file mode 100644 index 00000000..0d092374 --- /dev/null +++ b/tests/test_notify.c @@ -0,0 +1,179 @@ +#include +#include +#include +#include +#include "../src/webcfg_notify.h" +void free_notify_params_struct(notify_params_t *param); +void* processWebConfgNotification(); +#define UNUSED(x) (void )(x) +bool shutdown_flag = false; +char *test_mac=NULL; +char *get_deviceMAC(void) +{ + return test_mac; +} +char *global_payload=NULL; +char *global_source=NULL; +char *global_destination=NULL; +void sendNotification_rbus(char *payload, char *source, char *destination) +{ + global_payload = payload; + global_source = source; + global_destination = destination; +} + +void sendNotification(char *payload, char *source, char *destination) +{ + WebcfgDebug("B4 sendNotification_rbus\n"); + sendNotification_rbus(payload, source, destination); +} + +bool get_global_shutdown() +{ + return shutdown_flag; +} + +void test_getStatusErrorCodeAndMessage() +{ + char* result = NULL; + uint16_t ret = 0; + ret = getStatusErrorCodeAndMessage(DECODE_ROOT_FAILURE,&result); + CU_ASSERT_EQUAL(ret, 111); + CU_ASSERT_STRING_EQUAL(result, "decode_root_failure"); + + ret = getStatusErrorCodeAndMessage(INCORRECT_BLOB_TYPE,&result); + CU_ASSERT_EQUAL(ret, 211); + CU_ASSERT_STRING_EQUAL(result, "incorrect_blob_type"); + + ret = getStatusErrorCodeAndMessage(BLOB_PARAM_VALIDATION_FAILURE,&result); + CU_ASSERT_EQUAL(ret, 211); + CU_ASSERT_STRING_EQUAL(result, "blob_param_validation_failure"); + + ret = getStatusErrorCodeAndMessage(WEBCONFIG_DATA_EMPTY,&result); + CU_ASSERT_EQUAL(ret, 211); + CU_ASSERT_STRING_EQUAL(result, "webconfig_data_empty"); + + ret = getStatusErrorCodeAndMessage(MULTIPART_BOUNDARY_NULL,&result); + CU_ASSERT_EQUAL(ret, 211); + CU_ASSERT_STRING_EQUAL(result, "multipart_boundary_NULL"); + + ret = getStatusErrorCodeAndMessage(INVALID_CONTENT_TYPE,&result); + CU_ASSERT_EQUAL(ret, 211); + CU_ASSERT_STRING_EQUAL(result, "invalid_content_type"); + + ret = getStatusErrorCodeAndMessage(ADD_TO_CACHE_LIST_FAILURE,&result); + CU_ASSERT_EQUAL(ret, 311); + CU_ASSERT_STRING_EQUAL(result, "add_to_cache_list_failure"); + + ret = getStatusErrorCodeAndMessage(FAILED_TO_SET_BLOB,&result); + CU_ASSERT_EQUAL(ret, 311); + CU_ASSERT_STRING_EQUAL(result, "failed_to_set_blob"); + + ret = getStatusErrorCodeAndMessage(MULTIPART_CACHE_NULL,&result); + CU_ASSERT_EQUAL(ret, 311); + CU_ASSERT_STRING_EQUAL(result, "multipart_cache_NULL"); + + ret = getStatusErrorCodeAndMessage(AKER_SUBDOC_PROCESSING_FAILED,&result); + CU_ASSERT_EQUAL(ret, 411); + CU_ASSERT_STRING_EQUAL(result, "aker_subdoc_processing_failed"); + + ret = getStatusErrorCodeAndMessage(AKER_RESPONSE_PARSE_FAILURE,&result); + CU_ASSERT_EQUAL(ret, 411); + CU_ASSERT_STRING_EQUAL(result, "aker_response_parse_failure"); + + ret = getStatusErrorCodeAndMessage(INVALID_AKER_RESPONSE,&result); + CU_ASSERT_EQUAL(ret, 411); + CU_ASSERT_STRING_EQUAL(result, "invalid_aker_response"); + + ret = getStatusErrorCodeAndMessage(LIBPARODUS_RECEIVE_FAILURE,&result); + CU_ASSERT_EQUAL(ret, 411); + CU_ASSERT_STRING_EQUAL(result, "libparodus_receive_failure"); + + ret = getStatusErrorCodeAndMessage(COMPONENT_EVENT_PARSE_FAILURE,&result); + CU_ASSERT_EQUAL(ret, 511); + CU_ASSERT_STRING_EQUAL(result, "component_event_parse_failure"); + + ret = getStatusErrorCodeAndMessage(SUBDOC_RETRY_FAILED,&result); + CU_ASSERT_EQUAL(ret, 611); + CU_ASSERT_STRING_EQUAL(result, "subdoc_retry_failed"); + + ret = getStatusErrorCodeAndMessage(999,&result); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_STRING_EQUAL(result, "Unknown Error"); +} + +void test_addWebConfgNotifyMsg() +{ + test_mac = strdup("123456789000"); + addWebConfgNotifyMsg("moca",0,"xyz","none","1122334455",10,"req",400,"root",200); + shutdown_flag=1; + processWebConfgNotification(); + CU_ASSERT_STRING_EQUAL(global_source,"mac:123456789000"); + CU_ASSERT_STRING_EQUAL(global_destination,"event:subdoc-report/moca/mac:123456789000/req"); + CU_ASSERT_STRING_EQUAL(global_payload,"{\"device_id\":\"mac:123456789000\",\"namespace\":\"moca\",\"application_status\":\"xyz\",\"timeout\":10,\"error_code\":400,\"transaction_uuid\":\"1122334455\",\"version\":\"root\"}"); + addWebConfgNotifyMsg("moca",123,"xyz","none","1122334455",10,"req",400,"root",404); + addWebConfgNotifyMsg("moca",123,"xyz","none","1122334455",10,"req",400,"root",404); + processWebConfgNotification(); + CU_ASSERT_STRING_EQUAL(global_source,"mac:123456789000"); + CU_ASSERT_STRING_EQUAL(global_destination,"event:rootdoc-report/mac:123456789000/req"); + CU_ASSERT_STRING_EQUAL(global_payload,"{\"device_id\":\"mac:123456789000\",\"namespace\":\"moca\",\"application_status\":\"xyz\",\"timeout\":10,\"error_code\":400,\"http_status_code\":404,\"transaction_uuid\":\"1122334455\",\"version\":\"123\"}"); + shutdown_flag=0; +} + +void test_initWebConfigNotifyTask() +{ + +} + +void test_free_notify_params_struct() +{ + notify_params_t *msg = NULL; + free_notify_params_struct(msg); + msg = (notify_params_t *)malloc(sizeof(notify_params_t)); + memset(msg, 0, sizeof(notify_params_t)); + msg->name = strdup("test1"); + msg->application_status = strdup("test2"); + msg->error_details = strdup("test3"); + msg->version = strdup("test4"); + msg->transaction_uuid = strdup("test5"); + msg->type = strdup("test6"); + free_notify_params_struct(msg); + CU_ASSERT_EQUAL(msg->name, NULL); +} + +void add_suites( CU_pSuite *suite ) +{ + *suite = CU_add_suite( "tests", NULL, NULL ); + CU_add_test( *suite, "test getStatusErrorCodeAndMessage", test_getStatusErrorCodeAndMessage); + CU_add_test( *suite, "test free_notify_params_struct", test_free_notify_params_struct); + CU_add_test( *suite, "test addWebConfgNotifyMsg", test_addWebConfgNotifyMsg); + CU_add_test( *suite, "test initWebConfigNotifyTask", test_initWebConfigNotifyTask); +} + +int main( int argc, char *argv[] ) +{ + unsigned rv = 1; + CU_pSuite suite = NULL; + + (void ) argc; + (void ) argv; + + if( CUE_SUCCESS == CU_initialize_registry() ) { + add_suites( &suite ); + + if( NULL != suite ) { + CU_basic_set_mode( CU_BRM_VERBOSE ); + CU_basic_run_tests(); + printf( "\n" ); + CU_basic_show_failures( CU_get_failure_list() ); + printf( "\n\n" ); + rv = CU_get_number_of_tests_failed(); + } + + CU_cleanup_registry(); + + } + + return rv; +} + \ No newline at end of file diff --git a/tests/test_timer.c b/tests/test_timer.c new file mode 100644 index 00000000..68c3a86d --- /dev/null +++ b/tests/test_timer.c @@ -0,0 +1,324 @@ +#include +#include +#include +#include +#include +#include +#include "../src/webcfg_timer.h" +#include "../src/webcfg_log.h" + +#define UNUSED(x) (void )(x) +char *firmwareUpgradeStartTime=NULL; +char *firmwareUpgradeEndTime=NULL; + +long getTimeOffset(void) +{ + return 0; +} + +uint16_t generateRandomId() +{ + return 1; +} + +void set_global_supplementarySync(int value) +{ + UNUSED(value); +} + +//mock function to change upgradestarttime for null and different values +void setFirmwareUpgradeStartTime(char* startTime) +{ + firmwareUpgradeStartTime = startTime; +} + +void setFirmwareUpgradeEndTime(char* endTime) +{ + firmwareUpgradeEndTime = endTime; +} + +char *getFirmwareUpgradeStartTime(void) +{ + if (firmwareUpgradeStartTime != NULL) + { + return firmwareUpgradeStartTime; + } + else + { + return NULL; + } +} + +char *getFirmwareUpgradeEndTime(void) +{ + if (firmwareUpgradeEndTime != NULL) + { + return firmwareUpgradeEndTime; + } + else + { + return NULL; + } +} + +void test_printTime() +{ + long long timestamp = 1618232435; + char* formattedTime = printTime(timestamp); + WebcfgInfo("Formatted Time: %s\n", formattedTime); + CU_ASSERT_STRING_NOT_EQUAL(formattedTime,""); + + //to compare current time + long long currentTimestamp = (long long)time(NULL); + formattedTime = printTime(currentTimestamp); + // Get the current date and time in the expected format + time_t rawtime = currentTimestamp; + struct tm* ts = localtime(&rawtime); + char expectedTime[80]; + strftime(expectedTime, sizeof(expectedTime), "%a %y%m%d %H:%M:%S", ts); + // Compare the actual formatted time string with the expected string + CU_ASSERT_STRING_EQUAL(formattedTime, expectedTime); +} + +void test_initMaintenanceTimer() +{ + //default values + initMaintenanceTimer(); + CU_ASSERT_TRUE(get_global_maintenance_time() > 0); + + //starttime and endtime are different + setFirmwareUpgradeStartTime("3344"); + setFirmwareUpgradeEndTime("5544"); + initMaintenanceTimer(); + CU_ASSERT_TRUE(get_global_maintenance_time() > 0); + + //starttime equal to endtime + setFirmwareUpgradeStartTime("3344"); + setFirmwareUpgradeEndTime("3344"); + initMaintenanceTimer(); + CU_ASSERT_TRUE(get_global_maintenance_time() >= 3600 && get_global_maintenance_time() <= 14400); + + //starttime > endtime + setFirmwareUpgradeStartTime("6453"); + setFirmwareUpgradeEndTime("3344"); + initMaintenanceTimer(); + CU_ASSERT_TRUE(get_global_maintenance_time() >= 3600 && get_global_maintenance_time() <= 14400); +} + +void test_checkMaintenanceTimer() +{ + //Maintenance time equal to current time + int result = checkMaintenanceTimer(); + CU_ASSERT_EQUAL(1,result); + + //not equal + struct timespec rt; + clock_gettime(CLOCK_REALTIME, &rt); + long cur_time_in_sec = rt.tv_sec; + set_global_maintenance_time(cur_time_in_sec); + result = checkMaintenanceTimer(); + CU_ASSERT_EQUAL(result, 0); +} + +void test_getMaintenanceSyncSeconds() +{ + //count equal to 0 + int maintenance_count = 0; + int result = getMaintenanceSyncSeconds(maintenance_count); + + struct timespec ct; + clock_gettime(CLOCK_REALTIME, &ct); + long current_time_in_sec = getTimeInSeconds(ct.tv_sec + getTimeOffset()); + long expected_maintenance_secs = get_global_maintenance_time() - current_time_in_sec; + CU_ASSERT_EQUAL(result, expected_maintenance_secs); + + //count equal to 1 + maintenance_count = 1; + result = getMaintenanceSyncSeconds(maintenance_count); + clock_gettime(CLOCK_REALTIME, &ct); + current_time_in_sec = getTimeInSeconds(ct.tv_sec + getTimeOffset()); + long sec_to_12 = 86400 - current_time_in_sec; + expected_maintenance_secs = sec_to_12 + get_global_maintenance_time(); + CU_ASSERT_EQUAL(result, expected_maintenance_secs); +} + +void test_getTimeInSeconds() +{ + struct tm timeinfo; + timeinfo.tm_year = 2023-1900; // Years since 1900 (e.g., 2023) + timeinfo.tm_mon = 9; // Month (0 - 11, so 9 is October) + timeinfo.tm_mday = 25; // Day of the month (1 - 31) + timeinfo.tm_hour = 13; // Hour (0 - 23) + timeinfo.tm_min = 45; // Minutes (0 - 59) + timeinfo.tm_sec = 50; // Seconds (0 - 59) + timeinfo.tm_isdst = 0; //+1 Dayslight saving, 0 no dayloght saving, -1 i dont know. + + // Convert the time to a time_t value + time_t time_value = mktime(&timeinfo); + + if (time_value == -1) { + // Handle the case where mktime returns -1 + WebcfgError("Error: Invalid date or time\n"); + } + else + { + WebcfgInfo("time_value: %lld\n", (long long)time_value); + // Call the function with the defined time + long seconds_since_midnight = getTimeInSeconds(time_value); + WebcfgInfo("The value is %ld\n",seconds_since_midnight); + // Calculate the expected result for the given time + long expected_result = 13 * 3600 + 45 * 60 + 50; // 13 hours, 45 minutes, and 50 seconds since midnight + WebcfgInfo("The expected result is %ld\n",expected_result); + CU_ASSERT_EQUAL(seconds_since_midnight, expected_result); + } +} + +void test_updateRetryTimeDiff() +{ + struct timespec ct; + clock_gettime(CLOCK_REALTIME, &ct); + long long present_time = ct.tv_sec; + + //expire time 10 sec in the future + long long expiry_time = present_time + 10; + + //get_retry_timer > time_diff + set_retry_timer(60); + + //retry_timer updated but global_retry_timestamp remains same + int time_diff = updateRetryTimeDiff(expiry_time); + int expected_retry_timer = (get_retry_timer() > time_diff) ? time_diff : get_retry_timer(); + CU_ASSERT_EQUAL(get_retry_timer(), expected_retry_timer); + + //global_retry_timestamp equal to 0 + set_global_retry_timestamp(0); + time_diff = updateRetryTimeDiff(expiry_time); + + WebcfgInfo("The get_global_retry_timestamp is %ld\n",get_global_retry_timestamp()); + WebcfgInfo("The getTimeInSeconds is %ld\n",getTimeInSeconds(expiry_time + 900)); + CU_ASSERT_EQUAL(get_global_retry_timestamp(), getTimeInSeconds(present_time + 900)); +} + +void test_getRetryExpiryTimeout() +{ + // Get the actual retry timestamp using the function + long long retry_timestamp = getRetryExpiryTimeout(); + + struct timespec ct; + clock_gettime(CLOCK_REALTIME, &ct); + long long current_time = ct.tv_sec; + long long expected_retry_timestamp = current_time + 900; + + CU_ASSERT_EQUAL(retry_timestamp, expected_retry_timestamp); +} + +void test_checkRetryTimer_past() +{ + struct timespec ct; + clock_gettime(CLOCK_REALTIME, &ct); + long long current_time = ct.tv_sec; + long long past_timestamp = current_time - 10; + int result_past = checkRetryTimer(past_timestamp); + + CU_ASSERT_EQUAL(result_past, 1); +} + +void test_checkRetryTimer_future() +{ + struct timespec ct; + clock_gettime(CLOCK_REALTIME, &ct); + long long current_time = ct.tv_sec; + long long past_timestamp = current_time + 10; + int result_future = checkRetryTimer(past_timestamp); + CU_ASSERT_EQUAL(result_future, 0); +} + +void test_retrySyncSeconds() +{ + struct timespec ct; + clock_gettime(CLOCK_REALTIME, &ct); + long long current_time = 0; + current_time = ct.tv_sec; + long current_time_in_sec = getTimeInSeconds(current_time); + long retry_secs = 0; + retry_secs = get_global_retry_timestamp() - current_time_in_sec; + int get = retrySyncSeconds(); + WebcfgInfo("The value of retry is %ld\n",retry_secs); + CU_ASSERT_EQUAL(retry_secs,get); + + //retry_secs = 0 + set_global_retry_timestamp(current_time_in_sec); + get = retrySyncSeconds(); + CU_ASSERT_EQUAL(0,get); + + //retry_sec < 0 + set_global_retry_timestamp(current_time_in_sec - 10); + get = retrySyncSeconds(); + CU_ASSERT_EQUAL(0,get); +} + +void test_set_get_maintenance_time() +{ + long unixTimestamp = 1635626400; + set_global_maintenance_time(unixTimestamp); + + CU_ASSERT_EQUAL(unixTimestamp,get_global_maintenance_time()); +} + +void test_set_get_retry_timer() +{ + int time = 60; + set_retry_timer(time); + + CU_ASSERT_EQUAL(time, get_retry_timer()); +} + +void test_set_get_retry_timestamp() +{ + long retry_time = 1635626400; + set_global_retry_timestamp(retry_time); + + CU_ASSERT_EQUAL(retry_time, get_global_retry_timestamp()); +} + +void add_suites( CU_pSuite *suite ) +{ + *suite = CU_add_suite( "tests", NULL, NULL ); + CU_add_test( *suite, "test printTIme", test_printTime); + CU_add_test( *suite, "test initMaintenanceTimer", test_initMaintenanceTimer); + CU_add_test( *suite, "test checkMaintenanceTimer", test_checkMaintenanceTimer); + CU_add_test( *suite, "test getMaintenanceSyncSeconds", test_getMaintenanceSyncSeconds); + CU_add_test( *suite, "test getTimeInSeconds", test_getTimeInSeconds); + CU_add_test( *suite, "test updateRetryTimeDiff", test_updateRetryTimeDiff); + CU_add_test( *suite, "test getRetryExpiryTimeout", test_getRetryExpiryTimeout); + CU_add_test( *suite, "test checkRetryTimer_past", test_checkRetryTimer_past); + CU_add_test( *suite, "test checkRetryTimer_future", test_checkRetryTimer_future); + CU_add_test( *suite, "test retrySyncSeconds", test_retrySyncSeconds); + CU_add_test( *suite, "test set_get_maintenance_time", test_set_get_maintenance_time); + CU_add_test( *suite, "test set_get_retry_timer", test_set_get_retry_timer); + CU_add_test( *suite, "test set_get_retry_timestamp", test_set_get_retry_timestamp); +} + +int main( int argc, char *argv[] ) +{ + unsigned rv = 1; + CU_pSuite suite = NULL; + + (void ) argc; + (void ) argv; + + if( CUE_SUCCESS == CU_initialize_registry() ) { + add_suites( &suite ); + if( NULL != suite ) { + CU_basic_set_mode( CU_BRM_VERBOSE ); + CU_basic_run_tests(); + printf( "\n" ); + CU_basic_show_failures( CU_get_failure_list() ); + printf( "\n\n" ); + rv = CU_get_number_of_tests_failed(); + } + CU_cleanup_registry(); + } + return rv; +} +