From 78401a41e022a9a8f657cf77f7476ddcd3c70c53 Mon Sep 17 00:00:00 2001 From: Rangaiah Date: Mon, 30 Oct 2023 11:02:04 +0530 Subject: [PATCH] Add unit tests for webcfg_param.c --- tests/CMakeLists.txt | 2 +- tests/test_webcfgparam.c | 292 ++++++++++++++++++++++++++++++++++----- 2 files changed, 260 insertions(+), 34 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6c8b900d..c494000e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -59,7 +59,7 @@ endif (FEATURE_SUPPORT_AKER) #------------------------------------------------------------------------------- add_test(NAME test_webcfgparam COMMAND ${MEMORY_CHECK} ./test_webcfgparam) add_executable(test_webcfgparam test_webcfgparam.c ../src/webcfg_param.c ../src/webcfg_helpers.c) -target_link_libraries (test_webcfgparam -lcunit -lmsgpackc -lcimplog) +target_link_libraries (test_webcfgparam -lcunit -lmsgpackc -lcimplog -lcjson) target_link_libraries (test_webcfgparam gcov -Wl,--no-as-needed ) diff --git a/tests/test_webcfgparam.c b/tests/test_webcfgparam.c index ea01c7e3..32541297 100644 --- a/tests/test_webcfgparam.c +++ b/tests/test_webcfgparam.c @@ -18,57 +18,205 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include "../src/webcfg_param.h" -#define WEB_CFG_FILE "../../tests/webcfg-now100.bin" -int readFromFile(char **data, int *len) +int process_webcfgparam( webcfgparam_t *pm, msgpack_object *obj ); +static void packJsonString( cJSON *item, msgpack_packer *pk ); +static void packJsonNumber( cJSON *item, msgpack_packer *pk ); +static void packJsonArray( cJSON *item, msgpack_packer *pk, int isBlob ); +static void packJsonObject( cJSON *item, msgpack_packer *pk, int isBlob); +static void __msgpack_pack_string( msgpack_packer *pk, const void *string, size_t n ); +static int convertJsonToMsgPack(char *data, char **encodedData, int isBlob); + + +static int getItemsCount(cJSON *object) +{ + int count = 0; + while(object != NULL) + { + object = object->next; + count++; + } + return count; +} +static void __msgpack_pack_string( msgpack_packer *pk, const void *string, size_t n ) +{ + printf("%s:%s\n",__FUNCTION__,(char *)string); + msgpack_pack_str( pk, n ); + msgpack_pack_str_body( pk, string, n ); +} +static void packJsonString( cJSON *item, msgpack_packer *pk ) +{ + if(item->string != NULL) + { + __msgpack_pack_string(pk, item->string, strlen(item->string)); + } + __msgpack_pack_string(pk, item->valuestring, strlen(item->valuestring)); +} +static void packJsonNumber( cJSON *item, msgpack_packer *pk ) +{ + printf("%s:%d\n",__FUNCTION__,item->valueint); + if(item->string != NULL) + { + __msgpack_pack_string(pk, item->string, strlen(item->string)); + } + msgpack_pack_int(pk, item->valueint); +} +static void packJsonArray(cJSON *item, msgpack_packer *pk, int isBlob) +{ + int arraySize = cJSON_GetArraySize(item); + printf("%s:%s\n",__FUNCTION__, item->string); + if(item->string != NULL) + { + //printf("packing %s\n",item->string); + __msgpack_pack_string(pk, item->string, strlen(item->string)); + } + msgpack_pack_array( pk, arraySize ); + int i=0; + for(i=0; itype) & 0XFF) + { + case cJSON_Object: + packJsonObject(arrItem, pk, isBlob); + break; + } + } +} +int getEncodedBlob(char *data, char **encodedData) +{ + cJSON *jsonData=NULL; + int encodedDataLen = 0; + printf("------- %s -------\n",__FUNCTION__); + jsonData=cJSON_Parse(data); + if(jsonData != NULL) + { + msgpack_sbuffer sbuf1; + msgpack_packer pk1; + msgpack_sbuffer_init( &sbuf1 ); + msgpack_packer_init( &pk1, &sbuf1, msgpack_sbuffer_write ); + packJsonObject(jsonData, &pk1, 1); + if( sbuf1.data ) + { + *encodedData = ( char * ) malloc( sizeof( char ) * sbuf1.size ); + if( NULL != *encodedData ) + { + memcpy( *encodedData, sbuf1.data, sbuf1.size ); + } + encodedDataLen = sbuf1.size; + } + msgpack_sbuffer_destroy(&sbuf1); + cJSON_Delete(jsonData); + } + printf("------- %s -------\n",__FUNCTION__); + return encodedDataLen; +} +static void packBlobData(cJSON *item, msgpack_packer *pk ) +{ + char *blobData = NULL, *encodedBlob = NULL; + int len = 0; + printf("------ %s ------\n",__FUNCTION__); + blobData = strdup(item->valuestring); + if(strlen(blobData) > 0) + { + printf("%s\n",blobData); + len = getEncodedBlob(blobData, &encodedBlob); + printf("%s\n",encodedBlob); + } + __msgpack_pack_string(pk, item->string, strlen(item->string)); + __msgpack_pack_string(pk, encodedBlob, len); + free(encodedBlob); + free(blobData); + printf("------ %s ------\n",__FUNCTION__); +} +static void packJsonObject( cJSON *item, msgpack_packer *pk, int isBlob ) { - FILE *fp; - int ch_count = 0; - fp = fopen(WEB_CFG_FILE, "r+"); - if (fp == NULL) + printf("%s\n",__FUNCTION__); + cJSON *child = item->child; + msgpack_pack_map( pk, getItemsCount(child)); + while(child != NULL) { - printf("Failed to open file %s. provide the file\n", WEB_CFG_FILE); - return 0; + switch((child->type) & 0XFF) + { + case cJSON_String: + if(child->string != NULL && (strcmp(child->string, "value") == 0) && isBlob == 1) + { + packBlobData(child, pk); + } + else + { + packJsonString(child, pk); + } + break; + case cJSON_Number: + packJsonNumber(child, pk); + break; + case cJSON_Array: + packJsonArray(child, pk, isBlob); + break; + } + child = child->next; + } +} +static int convertJsonToMsgPack(char *data, char **encodedData, int isBlob) +{ + cJSON *jsonData=NULL; + int encodedDataLen = 0; + jsonData=cJSON_Parse(data); + if(jsonData != NULL) + { + msgpack_sbuffer sbuf; + msgpack_packer pk; + msgpack_sbuffer_init( &sbuf ); + msgpack_packer_init( &pk, &sbuf, msgpack_sbuffer_write ); + packJsonObject(jsonData, &pk, isBlob); + if( sbuf.data ) + { + *encodedData = ( char * ) malloc( sizeof( char ) * sbuf.size ); + if( NULL != *encodedData ) + { + memcpy( *encodedData, sbuf.data, sbuf.size ); + } + encodedDataLen = sbuf.size; + } + msgpack_sbuffer_destroy(&sbuf); + cJSON_Delete(jsonData); } - fseek(fp, 0, SEEK_END); - ch_count = ftell(fp); - fseek(fp, 0, SEEK_SET); - *data = (char *) malloc(sizeof(char) * (ch_count + 1)); - fread(*data, 1, ch_count,fp); - - *len = ch_count; - (*data)[ch_count] ='\0'; - fclose(fp); - return 1; + return encodedDataLen; } + void test_basic() { webcfgparam_t *pm = NULL; - int err, len=0, i=0; - - char *binfileData = NULL; - int status = -1; - printf("readFromFile\n"); - status = readFromFile(&binfileData , &len); + int err, i=0; + char *Data = "{\"parameters\": [{\"name\":\"Device.DeviceInfo.Test\",\"value\":\"false\",\"dataType\":3},{\"name\":\"Device.DeviceInfo.Test1\",\"value\":\"true\",\"dataType\":3}]}"; - printf("read status %d\n", status); - if(status) + char *encodedData = NULL; + int encodedLen = 0; + encodedLen = convertJsonToMsgPack(Data, &encodedData, 1); + if(encodedLen) { void* basicV; - basicV = ( void*)binfileData; + basicV = ( void*)encodedData; - pm = webcfgparam_convert( basicV, len+1 ); + pm = webcfgparam_convert( basicV, encodedLen+1 ); err = errno; - printf( "errno: %s\n", webcfgparam_strerror(err) ); + printf( "errno: %s\n", webcfgparam_strerror(err) ); + CU_ASSERT_FATAL( NULL != pm ); CU_ASSERT_FATAL( NULL != pm->entries ); - CU_ASSERT_FATAL( 100 == pm->entries_count ); - CU_ASSERT_STRING_EQUAL( "Device.DeviceInfo.X_RDKCENTRAL-COM_CloudUIEnable", pm->entries[0].name ); - CU_ASSERT_STRING_EQUAL( "false", pm->entries[0].value ); + CU_ASSERT_FATAL( 2 == pm->entries_count ); + CU_ASSERT_STRING_EQUAL( "Device.DeviceInfo.Test", pm->entries[0].name ); CU_ASSERT_FATAL( 3 == pm->entries[0].type ); for(i = 0; i < (int)pm->entries_count ; i++) { @@ -81,10 +229,87 @@ void test_basic() } } +void test_webcfgparam_strerror() +{ + const char *txt; + txt = webcfgparam_strerror(0); + CU_ASSERT_STRING_EQUAL(txt,"No errors."); + txt = webcfgparam_strerror(1); + CU_ASSERT_STRING_EQUAL(txt,"Out of memory."); + txt = webcfgparam_strerror(10); + CU_ASSERT_STRING_EQUAL(txt,"Unknown error."); +} + +void test_process_webcfgparam() +{ + msgpack_object *inner; + webcfgparam_t *pm; + int ret=0,err=0; + inner = malloc( sizeof(msgpack_object)); + pm = malloc( sizeof(webcfgparam_t)); + inner->via.array.size=0; + ret = process_webcfgparam(pm,inner); + CU_ASSERT_EQUAL(ret, 0); + + inner->via.array.size=1; + msgpack_object_array *array = &inner->via.array; + array->ptr = malloc( sizeof(msgpack_object)); + array->ptr->type = 0; + ret = process_webcfgparam(pm,inner); + CU_ASSERT_EQUAL(ret, -1); + err = errno; + const char *txt; + txt = webcfgparam_strerror(err); + printf( "errno: %s\n", txt ); + CU_ASSERT_STRING_EQUAL(txt,"Invalid 'parameters' array."); +} + +void test_process_params_invalid_datatype() +{ + int err; + char *encodedData = NULL; + int encodedLen = 0; + encodedLen = convertJsonToMsgPack("{\"parameters\": [{\"name\":\"Device.DeviceInfo.CloudUIEnable1\",\"value\":\"false\",\"dataType\":65536}]} ", &encodedData, 1); + if(encodedLen) + { + void* basicV; + basicV = ( void*)encodedData; + webcfgparam_convert( basicV, encodedLen+1 ); + err = errno; + const char *txt; + txt = webcfgparam_strerror(err); + CU_ASSERT_STRING_EQUAL(txt,"Invalid 'blob' object."); + } +} + +void test_process_params_notify_attribute() +{ + int err; + char *encodedData = NULL; + int encodedLen = 0; + webcfgparam_t *pm = NULL; + encodedLen = convertJsonToMsgPack("{\"parameters\": [{\"name\":\"Device.DeviceInfo.CloudUIEnable1\",\"value\":\"false\",\"notify_attribute\":3}]} ", &encodedData, 1); + if(encodedLen) + { + void* basicV; + basicV = ( void*)encodedData; + pm = webcfgparam_convert( basicV, encodedLen+1 ); + CU_ASSERT_FATAL( NULL != pm ); + err = errno; + const char *txt; + txt = webcfgparam_strerror(err); + CU_ASSERT_STRING_EQUAL(txt,"No errors."); + } +} + void add_suites( CU_pSuite *suite ) { *suite = CU_add_suite( "tests", NULL, NULL ); - CU_add_test( *suite, "Full", test_basic); + CU_add_test( *suite, "test webcfgparam_strerror", test_webcfgparam_strerror); + CU_add_test( *suite, "test process_webcfgparam", test_process_webcfgparam); + CU_add_test( *suite, "Full", test_basic); + CU_add_test( *suite, "test process_params", test_process_params_invalid_datatype); + CU_add_test( *suite, "test process_params", test_process_params_notify_attribute); } /*----------------------------------------------------------------------------*/ @@ -116,3 +341,4 @@ int main( int argc, char *argv[] ) return rv; } +