Skip to content

Commit

Permalink
Merge pull request #199 from nlrcomcast/RDKB-52297
Browse files Browse the repository at this point in the history
Add unit tests for webcfg_blob.c
  • Loading branch information
sadhyama authored Nov 7, 2023
2 parents f6f8adb + 16ad68d commit ce9f409
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ endif (WEBCONFIG_BIN_SUPPORT)

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

#-------------------------------------------------------------------------------
# test_blob
#-------------------------------------------------------------------------------
add_test(NAME test_blob COMMAND ${MEMORY_CHECK} ./test_blob)
add_executable(test_blob test_blob.c ../src/webcfg_blob.c)
target_link_libraries (test_blob -lcunit -lmsgpackc -lcimplog -lcjson -ltrower-base64 )

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

# Code coverage

add_custom_target(coverage
Expand Down Expand Up @@ -375,8 +384,9 @@ ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_timer.dir/__/src --output-file test_
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_notify.dir/__/src --output-file test_notify.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_generic.dir/__/src --output-file test_generic_pc.info

${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_generic_pc.dir/__/src --output-file test_generic_pc.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_blob.dir/__/src --output-file test_blob.info

COMMAND lcov
-a test_events_supp.info
Expand All @@ -394,6 +404,7 @@ COMMAND lcov
-a test_timer.info
-a test_notify.info
-a test_generic_pc.info
-a test_blob.info
--output-file coverage.info

COMMAND genhtml coverage.info
Expand Down
133 changes: 133 additions & 0 deletions tests/test_blob.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <base64.h>
#include <msgpack.h>
#include <CUnit/Basic.h>
#include "../src/webcfg_blob.h"
#include "../src/webcfg_log.h"
#define UNUSED(x) (void )(x)
bool rbus_enable = true;
bool isRbusEnabled()
{

return rbus_enable;
}
bool isRbusListener(char *subDoc)
{
UNUSED(subDoc);
return true;
}
char * base64blobencoder(char * blob_data, size_t blob_size )
{
char* b64buffer = NULL;
size_t encodeSize = -1;
WebcfgDebug("Data is %s\n", blob_data);
WebcfgDebug("-----------Start of Base64 Encode ------------\n");
encodeSize = b64_get_encoded_buffer_size(blob_size);
WebcfgDebug("encodeSize is %zu\n", encodeSize);
b64buffer = malloc(encodeSize + 1);
if(b64buffer != NULL)
{
memset( b64buffer, 0, sizeof( encodeSize )+1 );

b64_encode((uint8_t *)blob_data, blob_size, (uint8_t *)b64buffer);
b64buffer[encodeSize] = '\0' ;
}
return b64buffer;
}
void test_writeToFileData()
{
char* db_file_path = "test.txt";
char* data = NULL;
size_t size = 0;
// Failed to create/open file in root
CU_ASSERT_EQUAL(writeToFileData("/test.txt", data, size), 0);
// NULL data pointer
CU_ASSERT_EQUAL(writeToFileData(db_file_path, data, size), 0);
// with data
CU_ASSERT_EQUAL(writeToFileData(db_file_path, "data", 5), 1);
//Removing created test.txt file
if (remove(db_file_path) != 0) {
CU_FAIL("Failed to remove file");
}
}

void test_webcfg_appendeddoc()
{
char *appended_doc = NULL;
int embPackSize = 0;
uint16_t trans_id = 0;
appended_doc = webcfg_appendeddoc("subdoc1", 1234, "test", 5, &trans_id, &embPackSize);
CU_ASSERT_NOT_EQUAL(embPackSize,0);
CU_ASSERT_FATAL( 0 != appended_doc);
rbus_enable = false;
appended_doc = webcfg_appendeddoc("subdoc2", 1234, "test", 5, &trans_id, &embPackSize);
CU_ASSERT_NOT_EQUAL(embPackSize,0);
CU_ASSERT_FATAL( 0 != appended_doc);
rbus_enable = true;
}

void test_appendWebcfgEncodedData()
{
size_t embeddeddocPackSize = -1;
void *embeddeddocdata = NULL;
//encode data
embeddeddocPackSize = appendWebcfgEncodedData(&embeddeddocdata, (void *)"abcd", 4, (void *)"efgh", 5);
CU_ASSERT_STRING_EQUAL(embeddeddocdata,"dbcdefgh");
CU_ASSERT_EQUAL(embeddeddocPackSize,9);
//test Msgpack Map (fixmap) MAX size
unsigned char byte[5]="abcd";
byte[0]=0xff;
embeddeddocPackSize = appendWebcfgEncodedData(&embeddeddocdata, (void *)byte, 4, (void *)"efgh", 5);
CU_ASSERT_EQUAL(embeddeddocPackSize,-1);
}

void test_webcfg_pack_appenddoc()
{
size_t appenddocPackSize = -1;
appenddoc_t *appenddata = NULL;
void *appenddocdata = NULL;
appenddocPackSize = webcfg_pack_appenddoc(appenddata, &appenddocdata);
CU_ASSERT_EQUAL(appenddocPackSize,-1);
}

void add_suites( CU_pSuite *suite )
{
*suite = CU_add_suite( "tests", NULL, NULL );
CU_add_test( *suite, "test writeToFileData", test_writeToFileData);
CU_add_test( *suite, "test webcfg_appendeddoc", test_webcfg_appendeddoc);
CU_add_test( *suite, "test appendWebcfgEncodedData", test_appendWebcfgEncodedData);
CU_add_test( *suite, "test webcfg_pack_appenddoc", test_webcfg_pack_appenddoc);
}

/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
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 ce9f409

Please sign in to comment.