Skip to content

Commit

Permalink
Merge pull request #254 from nlrcomcast/libpd_unittest
Browse files Browse the repository at this point in the history
Added additional test cases for libpd.c
  • Loading branch information
sadhyama authored Dec 12, 2023
2 parents 51286b8 + f67139c commit e574643
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 6 deletions.
7 changes: 5 additions & 2 deletions source/app/libpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
#endif

#define CONTENT_TYPE_JSON "application/json"
#define DEVICE_PROPS_FILE "/etc/device.properties"
#ifdef BUILD_YOCTO
#define DEVICE_PROPS_FILE "/etc/device.properties"
#else
#define DEVICE_PROPS_FILE "/tmp/device.properties"
#endif
#define MAX_PARALLEL_THREADS 1
#define PARODUS_URL_DEFAULT "tcp://127.0.0.1:6666"
#define CLIENT_URL_DEFAULT "tcp://127.0.0.1:6667"
Expand Down Expand Up @@ -654,4 +658,3 @@ const char *rdk_logger_module_fetch(void)
{
return "LOG.RDK.WEBPA";
}

150 changes: 146 additions & 4 deletions tests/test_libpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <CUnit/Basic.h>

#include "../source/include/webpa_adapter.h"
Expand All @@ -30,14 +32,39 @@
#include <libparodus.h>

#define UNUSED(x) (void )(x)
#ifdef BUILD_YOCTO
#define DEVICE_PROPS_FILE "/etc/device.properties"
#else
#define DEVICE_PROPS_FILE "/tmp/device.properties"
#endif
/*----------------------------------------------------------------------------*/
/* File Scoped Variables */
/*----------------------------------------------------------------------------*/
extern libpd_instance_t current_instance;
extern int wakeUpFlag;
int numLoops=1;
/*----------------------------------------------------------------------------*/
/* Mocks */
/*----------------------------------------------------------------------------*/
int writeToDBFile(char *db_file_path, char *data, size_t size)
{
int file_descriptor1 = open(db_file_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file_descriptor1 != -1) {
if (data == NULL)
{
close(file_descriptor1);
return 0;
}
else
{
write(file_descriptor1, data, strlen(data));
close(file_descriptor1);
return 1;
}
}
return 0;
}

void clearTraceContext()
{

Expand All @@ -60,9 +87,21 @@ void processRequest(char *reqPayload, char *transactionId, char **resPayload, he
{
UNUSED(reqPayload);
UNUSED(transactionId);
UNUSED(resPayload);
if(reqPayload == NULL)
UNUSED(resPayload);
else
*resPayload = strdup("{\"parameters\":[{\"name\":\"Device.DeviceInfo.TestApp\",\"value\":\"ACTIVE\",\"dataType\":0,\"parameterCount\":1,\"message\":\"Success\"}],\"statusCode\":200}");
UNUSED(req_headers);
UNUSED(res_headers);
if(req_headers == NULL)
{
UNUSED(res_headers);
}
else
{
res_headers->count = req_headers->count;
res_headers->headers[0] = strdup(req_headers->headers[0]);
res_headers->headers[1] = strdup(req_headers->headers[1]);
}
}

int libparodus_init (libpd_instance_t *instance, libpd_cfg_t *libpd_cfg)
Expand All @@ -79,6 +118,18 @@ int libparodus_send (libpd_instance_t instance, wrp_msg_t *msg)
function_called();
return (int) mock();
}
wrp_msg_t *msg_tmp = NULL;
int libparodus_receive (libpd_instance_t instance, wrp_msg_t **msg, uint32_t ms)
{
UNUSED(instance);
if(msg_tmp == NULL)
UNUSED(msg);
else
*msg = msg_tmp;
UNUSED(ms);
function_called();
return (int) mock();
}

unsigned int sleep(unsigned int seconds)
{
Expand All @@ -97,11 +148,34 @@ unsigned int sleep(unsigned int seconds)

void test_libpd_client_mgr()
{
will_return(libparodus_init, (intptr_t)1);
expect_function_call(libparodus_init);
will_return(libparodus_init, (intptr_t)1);
expect_function_call(libparodus_init);
will_return(libparodus_init, (intptr_t)1);
expect_function_call(libparodus_init);
will_return(libparodus_init, (intptr_t)1);
expect_function_call(libparodus_init);
will_return(libparodus_init, (intptr_t)0);
expect_function_call(libparodus_init);
libpd_client_mgr();
}

void err_libpd_client_mgr()
{
char *parodus_url = "PARODUS_URL=tcp://127.0.0.1:6666\nWEBPA_CLIENT_URL=tcp://127.0.0.1:6667\n";
writeToDBFile(DEVICE_PROPS_FILE,NULL,0);
will_return(libparodus_init, (intptr_t)0);
expect_function_call(libparodus_init);
libpd_client_mgr();

writeToDBFile(DEVICE_PROPS_FILE,parodus_url,strlen(parodus_url));
will_return(libparodus_init, (intptr_t)0);
expect_function_call(libparodus_init);
libpd_client_mgr();
remove(DEVICE_PROPS_FILE);
}

void test_sendNotification()
{
char *source = (char *) malloc(sizeof(char)*16);
Expand Down Expand Up @@ -162,6 +236,68 @@ void err_getConnCloudStatus()
ret = getConnCloudStatus("");
assert_int_equal(-1,ret);
}

void test_parallelProcessTask()
{
numLoops = 1;
msg_tmp = (wrp_msg_t *)malloc(sizeof(wrp_msg_t));
memset(msg_tmp, 0, sizeof(wrp_msg_t));
msg_tmp->msg_type = WRP_MSG_TYPE__REQ;
msg_tmp->u.req.headers = (headers_t *)malloc(sizeof(headers_t));
msg_tmp->u.req.headers->count = 2;
msg_tmp->u.req.payload = strdup("{\"parameters\":[{\"name\":\"Device.DeviceInfo.TestingApp\",\"value\":\"ACTIVE\",\"dataType\":0,\"parameterCount\":1,\"message\":\"Success\"}],\"statusCode\":200}");
msg_tmp->u.req.transaction_uuid = strdup("RYjWZTaP4TTTkZ6HhwjGLA");
msg_tmp->u.req.headers->headers[0] = strdup("123");
msg_tmp->u.req.headers->headers[1] = strdup("xyz");
msg_tmp->u.req.source = strdup("dns:uvxyz.webpa.comcast.net");
msg_tmp->u.req.dest = strdup("mac:dcebxxxxxxxx/config");
will_return(libparodus_receive, (intptr_t)0);
expect_function_call(libparodus_receive);
will_return(libparodus_send, (intptr_t)0);
expect_function_call(libparodus_send);
parallelProcessTask(NULL);
}

void test_cloudstatus_parallelProcessTask()
{
numLoops = 1;
msg_tmp = (wrp_msg_t *)malloc(sizeof(wrp_msg_t));
memset(msg_tmp, 0, sizeof(wrp_msg_t));
msg_tmp->msg_type = WRP_MSG_TYPE__RETREIVE;
msg_tmp->u.crud.source = strdup("mac:5896xxxxxxxx/parodus/cloud-status");
msg_tmp->u.crud.transaction_uuid = strdup("f804ba0d-fd7e-42b0-a00e-27c669da4b6e");
msg_tmp->u.crud.status = 0;
msg_tmp->u.crud.payload = strdup("{\"cloud-status\":\"online\"}");
will_return(libparodus_receive, (intptr_t)0);
expect_function_call(libparodus_receive);
parallelProcessTask(NULL);
}
void err_parallelProcessTask()
{
numLoops = 2;
will_return(libparodus_receive, (intptr_t)1);
expect_function_call(libparodus_receive);
will_return(libparodus_receive, (intptr_t)2);
expect_function_call(libparodus_receive);
parallelProcessTask(NULL);
}

void test_rdk_logger_module_fetch()
{
rdk_logger_module_fetch();
}

void test_parsePayloadForStatus()
{
char *status = NULL;
status = parsePayloadForStatus(NULL);
assert_int_equal(status,NULL);
status = parsePayloadForStatus("{\"cloudstatus\":\"online\"}");
assert_int_equal(status,NULL);
status = parsePayloadForStatus("{\"cloud-status\":\"\"}");
assert_int_equal(status,NULL);
}

/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
Expand All @@ -170,11 +306,17 @@ int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_libpd_client_mgr),
cmocka_unit_test(err_libpd_client_mgr),
cmocka_unit_test(test_sendNotification),
cmocka_unit_test(err_sendNotification),
cmocka_unit_test(test_getConnCloudStatus),
cmocka_unit_test(test_getConnCloudStatusOffline),
cmocka_unit_test(err_getConnCloudStatus)
cmocka_unit_test(err_getConnCloudStatus),
cmocka_unit_test(test_parallelProcessTask),
cmocka_unit_test(test_cloudstatus_parallelProcessTask),
cmocka_unit_test(err_parallelProcessTask),
cmocka_unit_test(test_rdk_logger_module_fetch),
cmocka_unit_test(test_parsePayloadForStatus)
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down

0 comments on commit e574643

Please sign in to comment.