mirror of
https://github.com/outbackdingo/parodus.git
synced 2026-01-28 02:20:02 +00:00
@@ -12,7 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
set(SOURCES main.c mutex.c networking.c nopoll_helpers.c nopoll_handlers.c ParodusInternal.c
|
||||
string_helpers.c time.c config.c conn_interface.c connection.c spin_thread.c client_list.c service_alive.c upstream.c downstream.c thread_tasks.c)
|
||||
string_helpers.c time.c config.c conn_interface.c connection.c spin_thread.c client_list.c service_alive.c upstream.c downstream.c thread_tasks.c partners_check.c)
|
||||
|
||||
add_executable(parodus ${SOURCES})
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
#include "downstream.h"
|
||||
#include "upstream.h"
|
||||
#include "connection.h"
|
||||
#include "partners_check.h"
|
||||
#include "ParodusInternal.h"
|
||||
#include "config.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* External Functions */
|
||||
@@ -24,15 +24,13 @@
|
||||
*/
|
||||
void listenerOnMessage(void * msg, size_t msgSize)
|
||||
{
|
||||
int rv =0, i = 0;
|
||||
int rv =0;
|
||||
wrp_msg_t *message;
|
||||
char* destVal = NULL;
|
||||
char dest[32] = {'\0'};
|
||||
char *partnerId = NULL;
|
||||
int msgType;
|
||||
int bytes =0;
|
||||
int destFlag =0;
|
||||
int checkPartnerId = 0, matchFlag = 0;
|
||||
size_t size = 0;
|
||||
int resp_size = -1 ;
|
||||
const char *recivedMsg = NULL;
|
||||
@@ -59,33 +57,15 @@ void listenerOnMessage(void * msg, size_t msgSize)
|
||||
if(message->msg_type == WRP_MSG_TYPE__REQ)
|
||||
{
|
||||
ParodusPrint("numOfClients registered is %d\n", get_numOfClients());
|
||||
if(message->u.req.partner_ids !=NULL)
|
||||
int ret = validate_partner_id(message, NULL);
|
||||
if(ret < 0)
|
||||
{
|
||||
ParodusPrint("Validating partner_id\n");
|
||||
checkPartnerId = 1;
|
||||
partnerId = get_parodus_cfg()->partner_id;
|
||||
ParodusPrint("partnerId: %s\n",partnerId);
|
||||
ParodusPrint("partner_ids count is %lu\n",message->u.req.partner_ids->count);
|
||||
|
||||
for(i = 0; i < (int) message->u.req.partner_ids->count; i++)
|
||||
{
|
||||
if(strcmp(partnerId, message->u.req.partner_ids->partner_ids[i]) == 0)
|
||||
{
|
||||
ParodusInfo("partner_id match found\n");
|
||||
matchFlag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(matchFlag == 0)
|
||||
{
|
||||
response = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(response, "statusCode", 430);
|
||||
cJSON_AddStringToObject(response, "message", "Invalid partner_id");
|
||||
}
|
||||
response = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(response, "statusCode", 430);
|
||||
cJSON_AddStringToObject(response, "message", "Invalid partner_id");
|
||||
}
|
||||
|
||||
if((message->u.req.dest !=NULL) && ((matchFlag == 1 && checkPartnerId == 1) || checkPartnerId == 0))
|
||||
if((message->u.req.dest !=NULL) && (ret >= 0))
|
||||
{
|
||||
destVal = message->u.req.dest;
|
||||
strtok(destVal , "/");
|
||||
@@ -121,34 +101,34 @@ void listenerOnMessage(void * msg, size_t msgSize)
|
||||
}
|
||||
}
|
||||
|
||||
if(destFlag == 0 || (matchFlag == 0 && checkPartnerId == 1))
|
||||
if(destFlag == 0 || ret < 0)
|
||||
{
|
||||
resp_msg = (wrp_msg_t *)malloc(sizeof(wrp_msg_t));
|
||||
memset(resp_msg, 0, sizeof(wrp_msg_t));
|
||||
resp_msg = (wrp_msg_t *)malloc(sizeof(wrp_msg_t));
|
||||
memset(resp_msg, 0, sizeof(wrp_msg_t));
|
||||
|
||||
resp_msg ->msg_type = msgType;
|
||||
resp_msg ->u.req.source = message->u.req.dest;
|
||||
resp_msg ->u.req.dest = message->u.req.source;
|
||||
resp_msg ->u.req.transaction_uuid=message->u.req.transaction_uuid;
|
||||
resp_msg ->msg_type = msgType;
|
||||
resp_msg ->u.req.source = message->u.req.dest;
|
||||
resp_msg ->u.req.dest = message->u.req.source;
|
||||
resp_msg ->u.req.transaction_uuid=message->u.req.transaction_uuid;
|
||||
|
||||
if(response != NULL)
|
||||
if(response != NULL)
|
||||
{
|
||||
str = cJSON_PrintUnformatted(response);
|
||||
ParodusInfo("Payload Response: %s\n", str);
|
||||
|
||||
resp_msg ->u.req.payload = (void *)str;
|
||||
resp_msg ->u.req.payload_size = strlen(str);
|
||||
|
||||
ParodusPrint("msgpack encode\n");
|
||||
resp_size = wrp_struct_to( resp_msg, WRP_BYTES, &resp_bytes );
|
||||
if(resp_size > 0)
|
||||
{
|
||||
str = cJSON_PrintUnformatted(response);
|
||||
ParodusInfo("Payload Response: %s\n", str);
|
||||
|
||||
resp_msg ->u.req.payload = (void *)str;
|
||||
resp_msg ->u.req.payload_size = strlen(str);
|
||||
|
||||
ParodusPrint("msgpack encode\n");
|
||||
resp_size = wrp_struct_to( resp_msg, WRP_BYTES, &resp_bytes );
|
||||
if(resp_size > 0)
|
||||
{
|
||||
size = (size_t) resp_size;
|
||||
sendUpstreamMsgToServer(&resp_bytes, size);
|
||||
}
|
||||
free(str);
|
||||
size = (size_t) resp_size;
|
||||
sendUpstreamMsgToServer(&resp_bytes, size);
|
||||
}
|
||||
free(resp_msg);
|
||||
free(str);
|
||||
}
|
||||
free(resp_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
108
src/partners_check.c
Normal file
108
src/partners_check.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* @file partners_check.c
|
||||
*
|
||||
* @description This describes functions to validate partner_id.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#include "ParodusInternal.h"
|
||||
#include "config.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Macros */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Internal Functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* External functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int validate_partner_id(wrp_msg_t *msg, partners_t **partnerIds)
|
||||
{
|
||||
int matchFlag = 0, i = 0, count = 0;
|
||||
ParodusPrint("********* %s ********\n",__FUNCTION__);
|
||||
char *partnerId = get_parodus_cfg()->partner_id;
|
||||
if(strlen(partnerId) <= 0)
|
||||
{
|
||||
ParodusPrint("partner_id is not available to validate\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(msg->msg_type == WRP_MSG_TYPE__EVENT)
|
||||
{
|
||||
if(msg->u.event.partner_ids != NULL)
|
||||
{
|
||||
count = (int) msg->u.event.partner_ids->count;
|
||||
ParodusPrint("partner_ids count is %d\n",count);
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
if(strcmp(partnerId, msg->u.event.partner_ids->partner_ids[i]) == 0)
|
||||
{
|
||||
ParodusInfo("partner_id match found\n");
|
||||
matchFlag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(matchFlag != 1)
|
||||
{
|
||||
(*partnerIds) = (partners_t *) malloc(sizeof(partners_t));
|
||||
(*partnerIds)->count = count+1;
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
(*partnerIds)->partner_ids[i] = msg->u.event.partner_ids->partner_ids[i];
|
||||
ParodusPrint("(*partnerIds)->partner_ids[%d] : %s\n",i,(*partnerIds)->partner_ids[i]);
|
||||
}
|
||||
(*partnerIds)->partner_ids[count] = (char *) malloc(sizeof(char) * 64);
|
||||
strcpy((*partnerIds)->partner_ids[count], partnerId);
|
||||
ParodusPrint("(*partnerIds)->partner_ids[%d] : %s\n",count,(*partnerIds)->partner_ids[count]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ParodusPrint("partner_ids list is NULL\n");
|
||||
(*partnerIds) = (partners_t *) malloc(sizeof(partners_t));
|
||||
(*partnerIds)->count = 1;
|
||||
(*partnerIds)->partner_ids[0] = (char *) malloc(sizeof(char) * 64);
|
||||
strcpy((*partnerIds)->partner_ids[0], partnerId);
|
||||
ParodusPrint("(*partnerIds)->partner_ids[0] : %s\n",(*partnerIds)->partner_ids[0]);
|
||||
}
|
||||
}
|
||||
else if(msg->msg_type == WRP_MSG_TYPE__REQ)
|
||||
{
|
||||
if(msg->u.req.partner_ids != NULL)
|
||||
{
|
||||
count = (int) msg->u.req.partner_ids->count;
|
||||
ParodusPrint("partner_ids count is %d\n",count);
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
if(strcmp(partnerId, msg->u.req.partner_ids->partner_ids[i]) == 0)
|
||||
{
|
||||
ParodusInfo("partner_id match found\n");
|
||||
matchFlag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(matchFlag != 1)
|
||||
{
|
||||
ParodusError("Invalid partner_id %s\n",partnerId);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ParodusPrint("partner_ids list is NULL\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
34
src/partners_check.h
Normal file
34
src/partners_check.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @file partners_check.h
|
||||
*
|
||||
* @description This describes functions to validate partner_id.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#ifndef _PARTNERS_CHECK_H_
|
||||
#define _PARTNERS_CHECK_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "wrp-c.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Data Structures */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
int validate_partner_id(wrp_msg_t *msg, partners_t **partnerIds);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* _PARTNERS_CHECK_H_ */
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "ParodusInternal.h"
|
||||
#include "upstream.h"
|
||||
#include "config.h"
|
||||
#include "partners_check.h"
|
||||
#include "connection.h"
|
||||
#include "client_list.h"
|
||||
#include "nopoll_helpers.h"
|
||||
@@ -40,8 +41,6 @@ pthread_cond_t nano_con=PTHREAD_COND_INITIALIZER;
|
||||
/* External functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
void packMetaData()
|
||||
{
|
||||
char boot_time[256]={'\0'};
|
||||
@@ -158,7 +157,7 @@ void *processUpstreamMessage()
|
||||
int rv=-1, rc = -1;
|
||||
int msgType;
|
||||
wrp_msg_t *msg;
|
||||
void *appendData;
|
||||
void *appendData, *bytes;
|
||||
size_t encodedSize;
|
||||
reg_list_item_t *temp = NULL;
|
||||
int matchFlag = 0;
|
||||
@@ -189,8 +188,8 @@ void *processUpstreamMessage()
|
||||
//Extract serviceName and url & store it in a linked list for reg_clients
|
||||
if(get_numOfClients() !=0)
|
||||
{
|
||||
matchFlag = 0;
|
||||
ParodusPrint("matchFlag reset to %d\n", matchFlag);
|
||||
matchFlag = 0;
|
||||
ParodusPrint("matchFlag reset to %d\n", matchFlag);
|
||||
temp = get_global_node();
|
||||
while(temp!=NULL)
|
||||
{
|
||||
@@ -251,9 +250,40 @@ void *processUpstreamMessage()
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(msgType == WRP_MSG_TYPE__EVENT)
|
||||
{
|
||||
ParodusInfo(" Received upstream event data\n");
|
||||
partners_t *partnersList = NULL;
|
||||
|
||||
int ret = validate_partner_id(msg, &partnersList);
|
||||
if(ret == 1)
|
||||
{
|
||||
wrp_msg_t *eventMsg = (wrp_msg_t *) malloc(sizeof(wrp_msg_t));
|
||||
eventMsg->msg_type = msgType;
|
||||
eventMsg->u.event.content_type=msg->u.event.content_type;
|
||||
eventMsg->u.event.source=msg->u.event.source;
|
||||
eventMsg->u.event.dest=msg->u.event.dest;
|
||||
eventMsg->u.event.payload=msg->u.event.payload;
|
||||
eventMsg->u.event.payload_size=msg->u.event.payload_size;
|
||||
eventMsg->u.event.headers=msg->u.event.headers;
|
||||
eventMsg->u.event.metadata=msg->u.event.metadata;
|
||||
eventMsg->u.event.partner_ids = partnersList;
|
||||
|
||||
int size = wrp_struct_to( eventMsg, WRP_BYTES, &bytes );
|
||||
if(size > 0)
|
||||
{
|
||||
sendUpstreamMsgToServer(&bytes, size);
|
||||
}
|
||||
free(eventMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
sendUpstreamMsgToServer(&message->msg, message->len);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Sending to server for msgTypes 3, 4, 5, 6, 7, 8.
|
||||
//Sending to server for msgTypes 3, 5, 6, 7, 8.
|
||||
ParodusInfo(" Received upstream data with MsgType: %d\n", msgType);
|
||||
//Appending metadata with packed msg received from client
|
||||
if(metaPackSize > 0)
|
||||
@@ -324,4 +354,3 @@ void sendUpstreamMsgToServer(void **resp_bytes, size_t resp_size)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -114,14 +114,14 @@ target_link_libraries (test_createConnection ${PARODUS_COMMON_LIBS} -lcmocka)
|
||||
# test_client_list
|
||||
#-------------------------------------------------------------------------------
|
||||
add_test(NAME test_client_list COMMAND test_client_list)
|
||||
add_executable(test_client_list test_client_list.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c ${PARODUS_COMMON_SRC})
|
||||
add_executable(test_client_list test_client_list.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
|
||||
target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS})
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# test_service_alive
|
||||
#-------------------------------------------------------------------------------
|
||||
add_test(NAME test_service_alive COMMAND test_service_alive)
|
||||
add_executable(test_service_alive test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ${PARODUS_COMMON_SRC})
|
||||
add_executable(test_service_alive test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
|
||||
target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS})
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
@@ -166,6 +166,13 @@ add_test(NAME test_ParodusInternal COMMAND test_ParodusInternal)
|
||||
add_executable(test_ParodusInternal test_ParodusInternal.c ../src/ParodusInternal.c ../src/config.c ../src/string_helpers.c)
|
||||
target_link_libraries (test_ParodusInternal -lcmocka ${PARODUS_COMMON_LIBS} )
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# test_partners_check
|
||||
#-------------------------------------------------------------------------------
|
||||
add_test(NAME test_partners_check COMMAND test_partners_check)
|
||||
add_executable(test_partners_check test_partners_check.c ../src/partners_check.c)
|
||||
target_link_libraries (test_partners_check -lcmocka -lwrp-c -lcimplog -Wl,--no-as-needed -lrt)
|
||||
|
||||
if (INTEGRATION_TESTING)
|
||||
#-------------------------------------------------------------------------------
|
||||
# simple_connection test
|
||||
@@ -182,6 +189,7 @@ add_executable(simple_connection simple_connection.c ${PARODUS_COMMON_SRC}
|
||||
../src/connection.c
|
||||
../src/ParodusInternal.c
|
||||
../src/client_list.c
|
||||
../src/partners_check.c
|
||||
../src/service_alive.c)
|
||||
target_link_libraries (simple_connection ${PARODUS_COMMON_LIBS})
|
||||
|
||||
@@ -205,6 +213,7 @@ add_executable(simple simple.c
|
||||
../src/ParodusInternal.c
|
||||
../src/spin_thread.c
|
||||
../src/client_list.c
|
||||
../src/partners_check.c
|
||||
../src/service_alive.c)
|
||||
|
||||
target_link_libraries (simple ${PARODUS_COMMON_LIBS} gcov -lnopoll -lnanomsg )
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "../src/downstream.h"
|
||||
#include "../src/config.h"
|
||||
#include "../src/ParodusInternal.h"
|
||||
#include "../src/partners_check.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
@@ -53,12 +53,6 @@ reg_list_item_t * get_global_node(void)
|
||||
return (reg_list_item_t *) mock();
|
||||
}
|
||||
|
||||
ParodusCfg *get_parodus_cfg(void)
|
||||
{
|
||||
function_called();
|
||||
return (ParodusCfg*) mock();
|
||||
}
|
||||
|
||||
ssize_t wrp_to_struct( const void *bytes, const size_t length,
|
||||
const enum wrp_format fmt, wrp_msg_t **msg )
|
||||
{
|
||||
@@ -82,6 +76,13 @@ int nn_send (int s, const void *buf, size_t len, int flags)
|
||||
function_called();
|
||||
return (int) mock();
|
||||
}
|
||||
|
||||
int validate_partner_id(wrp_msg_t *msg, partners_t **partnerIds)
|
||||
{
|
||||
UNUSED(msg); UNUSED(partnerIds);
|
||||
function_called();
|
||||
return (int) mock();
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Tests */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -94,14 +95,11 @@ void test_listenerOnMessage()
|
||||
memset(head, 0, sizeof(reg_list_item_t));
|
||||
strcpy(head->service_name, "iot");
|
||||
strcpy(head->url, "tcp://10.0.0.1:6600");
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_numOfClients, 1);
|
||||
expect_function_call(get_numOfClients);
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
will_return(validate_partner_id, 1);
|
||||
expect_function_call(validate_partner_id);
|
||||
will_return(get_global_node, head);
|
||||
expect_function_call(get_global_node);
|
||||
will_return(nn_send, 20);
|
||||
@@ -115,10 +113,6 @@ void test_listenerOnMessageMultipleClients()
|
||||
{
|
||||
will_return(wrp_to_struct, 1);
|
||||
expect_function_calls(wrp_to_struct, 1);
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
reg_list_item_t *head2 = (reg_list_item_t *) malloc(sizeof(reg_list_item_t));
|
||||
memset(head2, 0, sizeof(reg_list_item_t));
|
||||
@@ -139,8 +133,8 @@ void test_listenerOnMessageMultipleClients()
|
||||
|
||||
will_return(get_numOfClients, 3);
|
||||
expect_function_call(get_numOfClients);
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
will_return(validate_partner_id, 0);
|
||||
expect_function_call(validate_partner_id);
|
||||
will_return(get_global_node, head);
|
||||
expect_function_call(get_global_node);
|
||||
will_return(nn_send, 20);
|
||||
@@ -164,15 +158,11 @@ void err_listenerOnMessageServiceUnavailable()
|
||||
{
|
||||
will_return(wrp_to_struct, 2);
|
||||
expect_function_calls(wrp_to_struct, 1);
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_numOfClients, 0);
|
||||
expect_function_call(get_numOfClients);
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
will_return(validate_partner_id, 0);
|
||||
expect_function_call(validate_partner_id);
|
||||
will_return(get_global_node, NULL);
|
||||
expect_function_call(get_global_node);
|
||||
expect_function_call(sendUpstreamMsgToServer);
|
||||
@@ -185,13 +175,10 @@ void err_listenerOnMessageInvalidPartnerId()
|
||||
will_return(wrp_to_struct, 2);
|
||||
expect_function_calls(wrp_to_struct, 1);
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
|
||||
will_return(get_numOfClients, 0);
|
||||
expect_function_call(get_numOfClients);
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
will_return(validate_partner_id, -1);
|
||||
expect_function_call(validate_partner_id);
|
||||
expect_function_call(sendUpstreamMsgToServer);
|
||||
|
||||
listenerOnMessage("Hello", 6);
|
||||
|
||||
205
tests/test_partners_check.c
Executable file
205
tests/test_partners_check.c
Executable file
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* Copyright 2010-2016 Comcast Cable Communications Management, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <malloc.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "../src/config.h"
|
||||
#include "../src/partners_check.h"
|
||||
#include "../src/ParodusInternal.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Mocks */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
ParodusCfg *get_parodus_cfg(void)
|
||||
{
|
||||
function_called();
|
||||
return (ParodusCfg*) mock();
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Tests */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
void test_validate_partner_id_for_req()
|
||||
{
|
||||
static partners_t partner_ids = {1,{"comcast"}};
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__REQ;
|
||||
msg->u.req.partner_ids = &partner_ids;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
int ret = validate_partner_id(msg, NULL);
|
||||
assert_int_equal(ret, 1);
|
||||
}
|
||||
|
||||
void test_validate_partner_id_for_req_listNULL()
|
||||
{
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__REQ;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
int ret = validate_partner_id(msg, NULL);
|
||||
assert_int_equal(ret, 1);
|
||||
}
|
||||
|
||||
void test_validate_partner_id_for_req_withoutId()
|
||||
{
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__REQ;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
int ret = validate_partner_id(msg, NULL);
|
||||
assert_int_equal(ret, 0);
|
||||
}
|
||||
|
||||
void err_validate_partner_id_for_req()
|
||||
{
|
||||
static partners_t partner_ids = {1,{"shaw"}};
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__REQ;
|
||||
msg->u.req.partner_ids = &partner_ids;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
int ret = validate_partner_id(msg, NULL);
|
||||
assert_int_equal(ret, -1);
|
||||
}
|
||||
|
||||
void test_validate_partner_id_for_event()
|
||||
{
|
||||
static partners_t partner_ids = {1,{"comcast"}};
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__EVENT;
|
||||
msg->u.event.partner_ids = &partner_ids;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
|
||||
partners_t *list = NULL;
|
||||
int ret = validate_partner_id(msg, &list);
|
||||
assert_int_equal(ret, 1);
|
||||
}
|
||||
|
||||
void test_validate_partner_id_for_event_listNULL()
|
||||
{
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__EVENT;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
partners_t *list = NULL;
|
||||
int ret = validate_partner_id(msg, &list);
|
||||
assert_int_equal(ret, 1);
|
||||
assert_int_equal(list->count, 1);
|
||||
assert_string_equal(list->partner_ids[0], "comcast");
|
||||
}
|
||||
|
||||
void err_validate_partner_id_for_event()
|
||||
{
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__EVENT;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
int ret = validate_partner_id(msg, NULL);
|
||||
assert_int_equal(ret, 0);
|
||||
}
|
||||
|
||||
void test_validate_partner_id_for_event_withoutId()
|
||||
{
|
||||
static partners_t partner_ids = {1,{"shaw"}};
|
||||
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
|
||||
memset(msg, 0, sizeof(wrp_msg_t));
|
||||
msg->msg_type = WRP_MSG_TYPE__EVENT;
|
||||
msg->u.event.partner_ids = &partner_ids;
|
||||
|
||||
ParodusCfg cfg;
|
||||
memset(&cfg, 0, sizeof(ParodusCfg));
|
||||
strcpy(cfg.partner_id, "comcast");
|
||||
|
||||
will_return(get_parodus_cfg, &cfg);
|
||||
expect_function_call(get_parodus_cfg);
|
||||
partners_t *list = NULL;
|
||||
int ret = validate_partner_id(msg, &list);
|
||||
assert_int_equal(ret, 1);
|
||||
assert_int_equal(list->count, 2);
|
||||
assert_string_equal(list->partner_ids[0], "shaw");
|
||||
assert_string_equal(list->partner_ids[1], "comcast");
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* External Functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_validate_partner_id_for_req),
|
||||
cmocka_unit_test(test_validate_partner_id_for_req_listNULL),
|
||||
cmocka_unit_test(test_validate_partner_id_for_req_withoutId),
|
||||
cmocka_unit_test(err_validate_partner_id_for_req),
|
||||
cmocka_unit_test(test_validate_partner_id_for_event),
|
||||
cmocka_unit_test(test_validate_partner_id_for_event_listNULL),
|
||||
cmocka_unit_test(test_validate_partner_id_for_event_withoutId),
|
||||
cmocka_unit_test(err_validate_partner_id_for_event),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "../src/config.h"
|
||||
#include "../src/client_list.h"
|
||||
#include "../src/ParodusInternal.h"
|
||||
#include "../src/partners_check.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
@@ -177,6 +178,13 @@ int nn_connect (int s, const char *addr)
|
||||
function_called();
|
||||
return (int)mock();
|
||||
}
|
||||
|
||||
int validate_partner_id(wrp_msg_t *msg, partners_t **partnerIds)
|
||||
{
|
||||
UNUSED(msg); UNUSED(partnerIds);
|
||||
function_called();
|
||||
return (int) mock();
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Tests */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -263,7 +271,10 @@ void test_processUpstreamMessage()
|
||||
will_return(wrp_to_struct, 12);
|
||||
expect_function_call(wrp_to_struct);
|
||||
|
||||
will_return(appendEncodedData, 12);
|
||||
will_return(validate_partner_id, 1);
|
||||
expect_function_call(validate_partner_id);
|
||||
|
||||
will_return(appendEncodedData, 100);
|
||||
expect_function_call(appendEncodedData);
|
||||
|
||||
expect_function_call(sendMessage);
|
||||
@@ -276,6 +287,40 @@ void test_processUpstreamMessage()
|
||||
free(temp);
|
||||
}
|
||||
|
||||
void test_processUpstreamMessageInvalidPartner()
|
||||
{
|
||||
numLoops = 1;
|
||||
metaPackSize = 20;
|
||||
UpStreamMsgQ = (UpStreamMsg *) malloc(sizeof(UpStreamMsg));
|
||||
UpStreamMsgQ->msg = "First Message";
|
||||
UpStreamMsgQ->len = 13;
|
||||
UpStreamMsgQ->next = (UpStreamMsg *) malloc(sizeof(UpStreamMsg));
|
||||
UpStreamMsgQ->next->msg = "Second Message";
|
||||
UpStreamMsgQ->next->len = 15;
|
||||
UpStreamMsgQ->next->next = NULL;
|
||||
|
||||
temp = (wrp_msg_t *) malloc(sizeof(wrp_msg_t));
|
||||
memset(temp,0,sizeof(wrp_msg_t));
|
||||
temp->msg_type = 4;
|
||||
|
||||
will_return(wrp_to_struct, 12);
|
||||
expect_function_call(wrp_to_struct);
|
||||
|
||||
will_return(validate_partner_id, 0);
|
||||
expect_function_call(validate_partner_id);
|
||||
|
||||
will_return(appendEncodedData, 100);
|
||||
expect_function_call(appendEncodedData);
|
||||
|
||||
expect_function_call(sendMessage);
|
||||
|
||||
expect_function_call(wrp_free_struct);
|
||||
will_return(nn_freemsg,1);
|
||||
expect_function_call(nn_freemsg);
|
||||
processUpstreamMessage();
|
||||
free(temp);
|
||||
}
|
||||
|
||||
void test_processUpstreamMessageRegMsg()
|
||||
{
|
||||
numLoops = 1;
|
||||
@@ -527,6 +572,7 @@ int main(void)
|
||||
cmocka_unit_test(err_handleUpstreamBindFailure),
|
||||
cmocka_unit_test(err_handleUpstreamSockFailure),
|
||||
cmocka_unit_test(test_processUpstreamMessage),
|
||||
cmocka_unit_test(test_processUpstreamMessageInvalidPartner),
|
||||
cmocka_unit_test(test_processUpstreamMessageRegMsg),
|
||||
cmocka_unit_test(test_processUpstreamMessageRegMsgNoClients),
|
||||
cmocka_unit_test(err_processUpstreamMessage),
|
||||
|
||||
Reference in New Issue
Block a user