Skip to content

Commit

Permalink
Add unit tests for webcfg_param.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nlrcomcast committed Oct 31, 2023
1 parent aad5ca7 commit 78401a4
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 34 deletions.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

Expand Down
292 changes: 259 additions & 33 deletions tests/test_webcfgparam.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,205 @@
#include <errno.h>
#include <stdio.h>
#include <CUnit/Basic.h>
#include <msgpack.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <cJSON.h>
#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; i<arraySize; i++)
{
cJSON *arrItem = cJSON_GetArrayItem(item, i);
switch((arrItem->type) & 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++)
{
Expand All @@ -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);
}

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -116,3 +341,4 @@ int main( int argc, char *argv[] )

return rv;
}

0 comments on commit 78401a4

Please sign in to comment.