Skip to content

Commit

Permalink
Added unit test cases for mqtt_generic.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nlrcomcast committed Sep 11, 2023
1 parent c0a6f94 commit 35dcd85
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
export LD_LIBRARY_PATH=${RBUS_INSTALL_DIR}/usr/lib:${LD_LIBRARY_PATH}
./tests/test_mqttcm_component -a
./tests/test_mqttcm_timer -a
./tests/test_mqttcm_generic -a
- name: Stop rtrouted
run: |
Expand Down
2 changes: 1 addition & 1 deletion src/mqttcm_webcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ char * createMqttPubHeader(char * payload, ssize_t * payload_len)
{
if(payload != NULL)
{
content_type = (char *) malloc(sizeof(char)*MAX_BUF_SIZE);
content_type = (char *) (sizeof(char)*MAX_BUF_SIZE);
if(content_type !=NULL)
{
snprintf(content_type, MAX_BUF_SIZE, "Content-type: application/json");
Expand Down
14 changes: 14 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ target_link_libraries (test_mqttcm_timer -lcunit -lmsgpackc -lcimplog -lrbus -lp

target_link_libraries (test_mqttcm_timer gcov -Wl,--no-as-needed )

#-------------------------------------------------------------------------------
# test_mqttcm_generic
#-------------------------------------------------------------------------------

add_test(NAME test_mqttcm_generic COMMAND ${MEMORY_CHECK} ./test_mqttcm_generic)
set(SOURCES test_mqttcm_generic.c ../src/mqttcm_generic.c)
add_executable(test_mqttcm_generic ${SOURCES})
target_link_libraries (test_mqttcm_generic -lcunit -lcimplog)

target_link_libraries (test_mqttcm_generic gcov -Wl,--no-as-needed )


# Code coverage

Expand All @@ -66,12 +77,15 @@ COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_mqttcm_component.dir/__/src --output-file test_mqttcm_component.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_mqttcm_timer.dir/__/src --output-file test_mqttcm_timer.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_mqttcm_generic.dir/__/src --output-file test_mqttcm_generic.info


COMMAND lcov
-a test_mqttcm_connect.info
-a test_mqttcm_component.info
-a test_mqttcm_timer.info
-a test_mqttcm_generic.info

COMMAND genhtml coverage.info
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
130 changes: 130 additions & 0 deletions tests/test_mqttcm_generic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "../src/mqttcm_generic.h"
#define MQTTCM_FREE(__x__) if(__x__ != NULL) { free((void*)(__x__)); __x__ = NULL;} else {printf("Trying to free null pointer\n");}

// Test function for Get_Mqtt_LocationId
void test_Get_Mqtt_LocationId(void)
{
char *pString = NULL;
pString = ( char * )malloc(50);
int result = Get_Mqtt_LocationId(pString);

CU_ASSERT_STRING_EQUAL(pString, "12345678927e9a892c670333");
CU_ASSERT_EQUAL(result, 0);
MQTTCM_FREE(pString);
}

// Test function for Get_Mqtt_ClientId
void test_Get_Mqtt_ClientId(void)
{
char *pString = Get_Mqtt_ClientId();
CU_ASSERT_STRING_EQUAL(pString, "123456789012");
}

// Test function for Get_Mqtt_Broker
void test_Get_Mqtt_Broker(void)
{
char *pString = NULL;
pString = ( char * )malloc(50);
int result = Get_Mqtt_Broker(pString);

CU_ASSERT_STRING_EQUAL(pString, "localhost");
CU_ASSERT_EQUAL(result, 0);
MQTTCM_FREE(pString);
}

// Test function for Get_Mqtt_Port
void test_Get_Mqtt_Port(void)
{
char *pString = NULL;
pString = ( char * )malloc(50);
int result = Get_Mqtt_Port(pString);

CU_ASSERT_STRING_EQUAL(pString, "123");
CU_ASSERT_EQUAL(result, 0);
MQTTCM_FREE(pString);
}

// Test function for rbus_GetValueFromDB
void test_rbus_GetValueFromDB(void)
{
char *pString1 = NULL;
char **pString2 = NULL;
pString1 =( char * )malloc(50);
pString2 =( char ** )malloc(50);
int result = rbus_GetValueFromDB(pString1,pString2);

CU_ASSERT_EQUAL(result, 0);
MQTTCM_FREE(pString1);
MQTTCM_FREE(pString2);
}

// Test function for rbus_StoreValueIntoDB
void test_rbus_StoreValueIntoDB(void)
{
char *pString1 = NULL;
char *pString2 = NULL;
pString1 = ( char * )malloc(50);
pString2 = ( char * )malloc(50);

int result = rbus_StoreValueIntoDB(pString1,pString2);

CU_ASSERT_EQUAL(result, 0);
MQTTCM_FREE(pString1);
MQTTCM_FREE(pString2);
}

int init_suite(void)
{
// Initialize any necessary resources or setups
return 0;
}

// Test suite cleanup function

int clean_suite(void)
{
// Clean up any allocated resources or memory
return 0;
}

void add_suites( CU_pSuite *suite )
{
*suite = CU_add_suite( "tests", NULL, NULL );
CU_add_test( *suite, "test Get_Mqtt_LocationId", test_Get_Mqtt_LocationId);
CU_add_test( *suite, "test Get_Mqtt_ClientId", test_Get_Mqtt_ClientId);
CU_add_test( *suite, "test Get_Mqtt_Broker", test_Get_Mqtt_Broker);
CU_add_test( *suite, "test Get_Mqtt_Port", test_Get_Mqtt_Port);
CU_add_test( *suite, "test rbus_GetValueFromDB", test_rbus_GetValueFromDB);
CU_add_test( *suite, "test rbus_StoreValueIntoDB", test_rbus_StoreValueIntoDB);
}

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;
}

0 comments on commit 35dcd85

Please sign in to comment.