Compare commits

...

2 Commits

Author SHA1 Message Date
selvam krishnamoorthy
4a8bb83f03 Merge pull request #115 from sadhyama/parodus_yocto
Cherry-picked all the required checkins to bring this to yocto stable
2017-06-23 15:25:16 +05:30
Gayathri
91739f63a9 Added negative test cases for mutex.c
Added partner_id to metadata

Added partner_id check for wrp msg_type EVENT

Added test for partners_check.c

To fix build issues on ubuntu 14.04 32 bit

Fixed broken Fedora build.

Integrate valgrind

Fix valgrind errors

To fix travis build failure due to log flooding

To fix nanomsg linking issue on fedura and ubuntu 32 bit

Added log for Last Reboot reason

Logs are modified as per webpa telemetry data

Added signal handler for parodus

Fixed parodus client reconnect issue due to delay in sending keep alive

Remove simple.c integration test cases
2017-06-23 12:58:23 +05:30
29 changed files with 810 additions and 1003 deletions

View File

@@ -21,12 +21,12 @@ before_install:
install:
- sudo apt-get update -qq
- sudo apt-get install -y -qq libcunit1 libcunit1-dev
- sudo apt-get install -y -qq libcunit1 libcunit1-dev valgrind
script:
- mkdir build
- cd build
- cmake .. -DINTEGRATION_TESTING:BOOL=false
- cmake .. -DINTEGRATION_TESTING:BOOL=false -DDISABLE_VALGRIND:BOOL=true
- make
- make test

View File

@@ -23,8 +23,8 @@ set(INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/_install)
set(PREFIX_DIR ${CMAKE_CURRENT_BINARY_DIR}/_prefix)
set(INCLUDE_DIR ${INSTALL_DIR}/include)
set(LIBRARY_DIR ${INSTALL_DIR}/lib)
set(NANOMSG_LIBRARY_DIR ${INSTALL_DIR}/lib/x86_64-linux-gnu)
set(LIBRARY_DIR64 ${INSTALL_DIR}/lib64)
set(COMMON_LIBRARY_DIR ${INSTALL_DIR}/lib/${CMAKE_LIBRARY_ARCHITECTURE})
set(TEST_RESULTS_DIR ${CMAKE_CURRENT_BINARY_DIR}/test_results)
file(MAKE_DIRECTORY ${TEST_RESULTS_DIR})
@@ -144,6 +144,7 @@ add_dependencies(libcimplog cimplog)
# wrp-c external dependency
#-------------------------------------------------------------------------------
ExternalProject_Add(wrp-c
DEPENDS trower-base64 msgpack cimplog
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/_prefix/wrp-c
GIT_REPOSITORY https://github.com/Comcast/wrp-c.git
GIT_TAG "master"
@@ -151,6 +152,9 @@ ExternalProject_Add(wrp-c
-DMSGPACK_ENABLE_CXX=OFF
-DMSGPACK_BUILD_EXAMPLES=OFF
-DBUILD_TESTING=OFF
-DMAIN_PROJ_BUILD=ON
-DMAIN_PROJ_LIB_PATH=${LIBRARY_DIR}
-DMAIN_PROJ_INCLUDE_PATH=${INCLUDE_DIR}
)
add_library(libwrp-c STATIC SHARED IMPORTED)
add_dependencies(libwrp-c wrp-c)
@@ -158,10 +162,16 @@ add_dependencies(libwrp-c wrp-c)
# libparodus external dependency
#-------------------------------------------------------------------------------
ExternalProject_Add(libparodus
DEPENDS trower-base64 msgpack nanomsg wrp-c
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/_prefix/libparodus
GIT_REPOSITORY https://github.com/Comcast/libparodus.git
GIT_TAG "master"
CMAKE_ARGS += -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}
-DMAIN_PROJ_BUILD=ON
-DMAIN_PROJ_LIB_PATH=${LIBRARY_DIR}
-DMAIN_PROJ_LIB64_PATH=${LIBRARY_DIR64}
-DMAIN_PROJ_COMMON_PATH=${COMMON_LIBRARY_DIR}
-DMAIN_PROJ_INCLUDE_PATH=${INCLUDE_DIR}
)
add_library(liblibparodus STATIC SHARED IMPORTED)
add_dependencies(liblibparodus libparodus)
@@ -182,8 +192,7 @@ endif (BUILD_TESTING)
endif ()
link_directories ( ${LIBRARY_DIR} ${LIBRARY_DIR64} ${NANOMSG_LIBRARY_DIR} )
link_directories ( ${LIBRARY_DIR} ${COMMON_LIBRARY_DIR} ${LIBRARY_DIR64} )
add_subdirectory(src)
if (BUILD_TESTING)
add_subdirectory(tests)

View File

@@ -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})

View File

@@ -97,6 +97,7 @@ char* getWebpaConveyHeader()
encodedData[j]='\0';
ParodusPrint("Encoded X-WebPA-Convey Header: [%zd]%s\n", strlen(encodedData), encodedData);
}
free(buffer);
cJSON_Delete(response);
return encodedData;
}

View File

@@ -32,6 +32,7 @@ extern "C" {
#define WEBPA_URL "webpa-url"
#define WEBPA_PING_TIMEOUT "webpa-ping-timeout"
#define WEBPA_BACKOFF_MAX "webpa-backoff-max"
#define PARTNER_ID "partner-id"
#define WEBPA_PROTOCOL_VALUE "WebPA-1.6"
#define WEBPA_PATH_URL "/api/v2/device"

View File

@@ -81,6 +81,7 @@ void createSocketConnection(void *config_in, void (* initKeypress)())
if(!close_retry)
{
ParodusError("ping wait time > %d. Terminating the connection with WebPA server and retrying\n", get_parodus_cfg()->webpa_ping_timeout);
ParodusInfo("Reconnect detected, setting Ping_Miss reason for Reconnect\n");
set_global_reconnect_reason("Ping_Miss");
LastReasonStatus = true;
pthread_mutex_lock (&close_mut);

View File

@@ -99,6 +99,7 @@ int createNopollConnection(noPollCtx *ctx)
headerValues[1] = "wrp-0.11,getset-0.1";
ParodusPrint("BootTime In sec: %d\n", get_parodus_cfg()->boot_time);
ParodusInfo("Received reboot_reason as:%s\n", get_parodus_cfg()->hw_last_reboot_reason);
ParodusInfo("Received reconnect_reason as:%s\n", reconnect_reason);
snprintf(user_agent, sizeof(user_agent),
"%s (%s; %s/%s;)",

View File

@@ -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;
@@ -55,39 +53,24 @@ void listenerOnMessage(void * msg, size_t msgSize)
ParodusPrint("\nDecoded recivedMsg of size:%d\n", rv);
msgType = message->msg_type;
ParodusInfo("msgType received:%d\n", msgType);
if(message->msg_type == WRP_MSG_TYPE__AUTH)
{
ParodusInfo("Authorization Status received with Status code :%d\n", message->u.auth.status);
}
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");
str = cJSON_PrintUnformatted(response);
ParodusInfo("Payload Response: %s\n", str);
}
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 , "/");
@@ -120,20 +103,24 @@ void listenerOnMessage(void * msg, size_t msgSize)
response = cJSON_CreateObject();
cJSON_AddNumberToObject(response, "statusCode", 531);
cJSON_AddStringToObject(response, "message", "Service Unavailable");
str = cJSON_PrintUnformatted(response);
ParodusInfo("Payload Response: %s\n", str);
}
}
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;
if(response != NULL)
{
str = cJSON_PrintUnformatted(response);
ParodusInfo("Payload Response: %s\n", str);
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 ->u.req.payload = (void *)str;
resp_msg ->u.req.payload_size = strlen(str);
@@ -145,7 +132,11 @@ void listenerOnMessage(void * msg, size_t msgSize)
sendUpstreamMsgToServer(&resp_bytes, size);
}
free(str);
free(resp_msg);
cJSON_Delete(response);
free(resp_bytes);
resp_bytes = NULL;
}
free(resp_msg);
}
}
}

View File

@@ -16,10 +16,11 @@
*/
#include <string.h>
#include "stdlib.h"
#include "config.h"
#include "conn_interface.h"
#include "parodus_log.h"
#include "signal.h"
/*----------------------------------------------------------------------------*/
/* Macros */
@@ -39,13 +40,26 @@
/*----------------------------------------------------------------------------*/
/* Function Prototypes */
/*----------------------------------------------------------------------------*/
/* none */
static void sig_handler(int sig);
/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
int main( int argc, char **argv)
{
signal(SIGTERM, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
signal(SIGSEGV, sig_handler);
signal(SIGBUS, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGFPE, sig_handler);
signal(SIGILL, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGALRM, sig_handler);
ParodusCfg parodusCfg;
memset(&parodusCfg,0,sizeof(parodusCfg));
@@ -65,4 +79,43 @@ const char *rdk_logger_module_fetch(void)
/*----------------------------------------------------------------------------*/
/* Internal functions */
/*----------------------------------------------------------------------------*/
/* none */
static void sig_handler(int sig)
{
if ( sig == SIGINT )
{
signal(SIGINT, sig_handler); /* reset it to this function */
ParodusInfo("SIGINT received!\n");
exit(0);
}
else if ( sig == SIGUSR1 )
{
signal(SIGUSR1, sig_handler); /* reset it to this function */
ParodusInfo("SIGUSR1 received!\n");
}
else if ( sig == SIGUSR2 )
{
ParodusInfo("SIGUSR2 received!\n");
}
else if ( sig == SIGCHLD )
{
signal(SIGCHLD, sig_handler); /* reset it to this function */
ParodusInfo("SIGHLD received!\n");
}
else if ( sig == SIGPIPE )
{
signal(SIGPIPE, sig_handler); /* reset it to this function */
ParodusInfo("SIGPIPE received!\n");
}
else if ( sig == SIGALRM )
{
signal(SIGALRM, sig_handler); /* reset it to this function */
ParodusInfo("SIGALRM received!\n");
}
else
{
ParodusInfo("Signal %d received!\n", sig);
exit(0);
}
}

View File

@@ -119,11 +119,13 @@ void listenerOnCloseMessage (noPollCtx * ctx, noPollConn * conn, noPollPtr user_
if((user_data != NULL) && (strstr(user_data, "SSL_Socket_Close") != NULL) && !LastReasonStatus)
{
ParodusInfo("Reconnect detected, setting Reconnect reason as Server close\n");
set_global_reconnect_reason("Server_closed_connection");
LastReasonStatus = true;
}
else if ((user_data == NULL) && !LastReasonStatus)
{
ParodusInfo("Reconnect detected, setting Reconnect reason as Unknown\n");
set_global_reconnect_reason("Unknown");
}

View File

@@ -70,7 +70,7 @@ int sendResponse(noPollConn * conn, void * buffer, size_t length)
{
if (-1 == bytes_sent || (bytes_sent = nopoll_conn_flush_writes(conn, FLUSH_WAIT_TIME, bytes_sent)) != len_to_send)
{
ParodusPrint("sendResponse() Failed to send all the data\n");
ParodusError("sendResponse() Failed to send all the data\n");
cp = NULL;
break;
}

108
src/partners_check.c Normal file
View 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
View 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_ */

View File

@@ -82,7 +82,7 @@ void *serviceAliveTask()
else
{
ParodusInfo("No clients are registered, waiting ..\n");
sleep(70);
sleep(50);
}
}
}

View File

@@ -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"
@@ -16,7 +17,7 @@
/*----------------------------------------------------------------------------*/
/* Macros */
/*----------------------------------------------------------------------------*/
#define METADATA_COUNT 11
#define METADATA_COUNT 12
/*----------------------------------------------------------------------------*/
/* File Scoped Variables */
@@ -40,8 +41,6 @@ pthread_cond_t nano_con=PTHREAD_COND_INITIALIZER;
/* External functions */
/*----------------------------------------------------------------------------*/
void packMetaData()
{
char boot_time[256]={'\0'};
@@ -60,7 +59,8 @@ void packMetaData()
{LAST_RECONNECT_REASON, get_global_reconnect_reason()},
{WEBPA_PROTOCOL, get_parodus_cfg()->webpa_protocol},
{WEBPA_UUID,get_parodus_cfg()->webpa_uuid},
{WEBPA_INTERFACE, get_parodus_cfg()->webpa_interface_used}
{WEBPA_INTERFACE, get_parodus_cfg()->webpa_interface_used},
{PARTNER_ID, get_parodus_cfg()->partner_id}
};
const data_t metapack = {METADATA_COUNT, meta_pack};
@@ -157,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;
@@ -188,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)
{
@@ -250,9 +250,42 @@ 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);
free(bytes);
bytes = NULL;
}
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)
@@ -323,4 +356,3 @@ void sendUpstreamMsgToServer(void **resp_bytes, size_t resp_size)
}
}

View File

@@ -19,26 +19,30 @@ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-cov
set (PARODUS_COMMON_SRC ../src/string_helpers.c ../src/mutex.c ../src/time.c ../src/config.c ../src/spin_thread.c)
set (PARODUS_COMMON_LIBS gcov -lcunit -lcimplog -lwrp-c -lpthread -lm -lmsgpackc -lcjson -ltrower-base64 -lnopoll -lnanomsg -Wl,--no-as-needed -lrt)
if(NOT DISABLE_VALGRIND)
set (MEMORY_CHECK valgrind --leak-check=full --show-reachable=yes -v)
endif ()
link_directories ( ${LIBRARY_DIR} )
#-------------------------------------------------------------------------------
# test_mutex
#-------------------------------------------------------------------------------
add_test(NAME test_mutex COMMAND test_mutex)
add_test(NAME test_mutex COMMAND ${MEMORY_CHECK} ./test_mutex)
add_executable(test_mutex test_mutex.c ../src/mutex.c)
target_link_libraries (test_mutex ${PARODUS_COMMON_LIBS})
target_link_libraries (test_mutex ${PARODUS_COMMON_LIBS} -lcmocka)
#-------------------------------------------------------------------------------
# test_networking
#-------------------------------------------------------------------------------
add_test(NAME test_networking COMMAND test_networking)
add_test(NAME test_networking COMMAND ${MEMORY_CHECK} ./test_networking)
add_executable(test_networking test_networking.c ../src/networking.c)
target_link_libraries (test_networking ${PARODUS_COMMON_LIBS})
#-------------------------------------------------------------------------------
# test_nopoll_helpers
#-------------------------------------------------------------------------------
add_test(NAME test_nopoll_helpers COMMAND test_nopoll_helpers)
add_test(NAME test_nopoll_helpers COMMAND ${MEMORY_CHECK} ./test_nopoll_helpers)
add_executable(test_nopoll_helpers test_nopoll_helpers.c ../src/nopoll_helpers.c)
target_link_libraries (test_nopoll_helpers -Wl,--no-as-needed -lrt -lcmocka -lcimplog -lnopoll)
@@ -62,113 +66,120 @@ target_link_libraries (libpd_test
#-------------------------------------------------------------------------------
# test_time
#-------------------------------------------------------------------------------
add_test(NAME test_time COMMAND test_time)
add_test(NAME test_time COMMAND ${MEMORY_CHECK} ./test_time)
add_executable(test_time test_time.c ../src/time.c)
target_link_libraries (test_time ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_spin_thread error
#-------------------------------------------------------------------------------
add_test(NAME test_spin_thread_e COMMAND test_spin_thread_e)
add_test(NAME test_spin_thread_e COMMAND ${MEMORY_CHECK} ./test_spin_thread_e)
add_executable(test_spin_thread_e test_spin_thread_e.c ../src/spin_thread.c)
target_link_libraries (test_spin_thread_e ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_spin_thread success
#-------------------------------------------------------------------------------
add_test(NAME test_spin_thread_s COMMAND test_spin_thread_s)
add_test(NAME test_spin_thread_s COMMAND ${MEMORY_CHECK} ./test_spin_thread_s)
add_executable(test_spin_thread_s test_spin_thread_s.c ../src/spin_thread.c)
target_link_libraries (test_spin_thread_s ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_string_helpers
#-------------------------------------------------------------------------------
add_test(NAME test_string_helpers COMMAND test_string_helpers)
add_test(NAME test_string_helpers COMMAND ${MEMORY_CHECK} ./test_string_helpers)
add_executable(test_string_helpers test_string_helpers.c ../src/string_helpers.c)
target_link_libraries (test_string_helpers ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_nopoll_handlers
#-------------------------------------------------------------------------------
add_test(NAME test_nopoll_handlers COMMAND test_nopoll_handlers)
add_test(NAME test_nopoll_handlers COMMAND ${MEMORY_CHECK} ./test_nopoll_handlers)
add_executable(test_nopoll_handlers test_nopoll_handlers.c ../src/nopoll_handlers.c)
target_link_libraries (test_nopoll_handlers -lnopoll -lcunit -lcimplog -Wl,--no-as-needed -lrt -lpthread -lm)
#-------------------------------------------------------------------------------
# test_connection
#-------------------------------------------------------------------------------
add_test(NAME test_connection COMMAND test_connection)
add_test(NAME test_connection COMMAND ${MEMORY_CHECK} ./test_connection)
add_executable(test_connection test_connection.c ../src/connection.c ${PARODUS_COMMON_SRC})
target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka)
#-------------------------------------------------------------------------------
# test_connection - function createNopollConnection
#-------------------------------------------------------------------------------
add_test(NAME test_createConnection COMMAND test_createConnection)
add_test(NAME test_createConnection COMMAND ${MEMORY_CHECK} ./test_createConnection)
add_executable(test_createConnection test_createConnection.c ../src/connection.c ../src/string_helpers.c ../src/config.c)
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_test(NAME test_client_list COMMAND ${MEMORY_CHECK} ./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 ../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_test(NAME test_service_alive COMMAND ${MEMORY_CHECK} ./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 ../src/partners_check.c ${PARODUS_COMMON_SRC})
target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS})
#-------------------------------------------------------------------------------
# test_config
#-------------------------------------------------------------------------------
add_test(NAME test_config COMMAND test_config)
add_test(NAME test_config COMMAND ${MEMORY_CHECK} ./test_config)
add_executable(test_config test_config.c ../src/config.c ../src/string_helpers.c)
target_link_libraries (test_config -lcmocka -lm -Wl,--no-as-needed -lrt -lcimplog)
#-------------------------------------------------------------------------------
# test_upstream
#-------------------------------------------------------------------------------
add_test(NAME test_upstream COMMAND test_upstream)
add_test(NAME test_upstream COMMAND ${MEMORY_CHECK} ./test_upstream)
add_executable(test_upstream test_upstream.c ../src/upstream.c)
target_link_libraries (test_upstream -lcmocka ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_downstream
#-------------------------------------------------------------------------------
add_test(NAME test_downstream COMMAND test_downstream)
add_test(NAME test_downstream COMMAND ${MEMORY_CHECK} ./test_downstream)
add_executable(test_downstream test_downstream.c ../src/downstream.c ../src/string_helpers.c)
target_link_libraries (test_downstream -lcmocka ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_thread_tasks
#-------------------------------------------------------------------------------
add_test(NAME test_thread_tasks COMMAND test_thread_tasks)
add_test(NAME test_thread_tasks COMMAND ${MEMORY_CHECK} ./test_thread_tasks)
add_executable(test_thread_tasks test_thread_tasks.c ../src/thread_tasks.c)
target_link_libraries (test_thread_tasks -lcmocka ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_conn_interface
#-------------------------------------------------------------------------------
add_test(NAME test_conn_interface COMMAND test_conn_interface)
add_test(NAME test_conn_interface COMMAND ${MEMORY_CHECK} ./test_conn_interface)
add_executable(test_conn_interface test_conn_interface.c ../src/conn_interface.c ../src/config.c ../src/string_helpers.c ../src/mutex.c)
target_link_libraries (test_conn_interface -lcmocka ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_ParodusInternal
#-------------------------------------------------------------------------------
add_test(NAME test_ParodusInternal COMMAND test_ParodusInternal)
add_test(NAME test_ParodusInternal COMMAND ${MEMORY_CHECK} ./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 ${MEMORY_CHECK} ./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 ${PARODUS_COMMON_LIBS})
if (INTEGRATION_TESTING)
#-------------------------------------------------------------------------------
# simple_connection test
#-------------------------------------------------------------------------------
add_test(NAME simple_connection COMMAND simple_connection)
add_test(NAME simple_connection COMMAND ${MEMORY_CHECK} ./simple_connection)
add_executable(simple_connection simple_connection.c ${PARODUS_COMMON_SRC}
../src/upstream.c
../src/conn_interface.c
@@ -180,30 +191,8 @@ 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})
#-------------------------------------------------------------------------------
# simple test
#-------------------------------------------------------------------------------
add_test(NAME simple COMMAND simple)
add_executable(simple simple.c
../src/upstream.c
../src/conn_interface.c
../src/downstream.c
../src/thread_tasks.c
../src/networking.c
../src/nopoll_helpers.c
../src/nopoll_handlers.c
../src/string_helpers.c
../src/mutex.c
../src/time.c
../src/config.c
../src/connection.c
../src/ParodusInternal.c
../src/spin_thread.c
../src/client_list.c
../src/service_alive.c)
target_link_libraries (simple ${PARODUS_COMMON_LIBS} gcov -lnopoll -lnanomsg )
endif (INTEGRATION_TESTING)

View File

@@ -1,712 +0,0 @@
/**
* 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 <stdarg.h>
#include <CUnit/Basic.h>
#include <stdbool.h>
#include <assert.h>
#include <nopoll.h>
//#include <nanomsg/bus.h>
#include "../src/ParodusInternal.h"
#include "../src/config.h"
#include "../src/nopoll_helpers.h"
#include "../src/downstream.h"
#include "../src/upstream.h"
#include<errno.h>
/* Nanomsg related Macros */
#define ENDPOINT "tcp://127.0.0.1:6666"
#define CLIENT1_URL "tcp://127.0.0.1:6667"
#define CLIENT2_URL "tcp://127.0.0.1:6668"
#define CLIENT3_URL "tcp://127.0.0.1:6669"
#define HTTP_CUSTOM_HEADER_COUNT 4
static void send_nanomsg_upstream(void **buf, int size);
void *handle_testsuites();
headers_t headers = { 2, {"Header 1", "Header 2"}};
void test_nanomsg_client_registration1()
{
/*****Test svc registation for nanomsg client1 ***/
ParodusInfo("test_nanomsg_client_registration1\n");
const wrp_msg_t reg = { .msg_type = WRP_MSG_TYPE__SVC_REGISTRATION,
.u.reg.service_name = "iot",
.u.reg.url = CLIENT1_URL};
void *bytes;
int size =0;
int rv1, rc;
wrp_msg_t *msg1;
int sock, bind;
int byte =0;
int t=25000;
// msgpack encode
ParodusPrint("msgpack encode\n");
size = wrp_struct_to( &reg, WRP_BYTES, &bytes );
/*** Enable this to decode and verify upstream registration msg **/
/***
rv = wrp_to_struct(bytes, size, WRP_BYTES, &message);
ParodusPrint("decoded msgType:%d\n", message->msg_type);
ParodusPrint("decoded service_name:%s\n", message->u.reg.service_name);
ParodusPrint("decoded dest:%s\n", message->u.reg.url);
wrp_free_struct(message);
***/
//nanomsg socket
sock = nn_socket (AF_SP, NN_PUSH);
int connect = nn_connect (sock, ENDPOINT);
CU_ASSERT(connect >= 0);
rc = nn_setsockopt(sock, NN_SOL_SOCKET, NN_SNDTIMEO, &t, sizeof(t));
CU_ASSERT(rc >= 0);
byte = nn_send (sock, bytes, size, 0);
ParodusInfo("----->Expected byte to be sent:%d\n", size);
ParodusInfo("----->actual byte sent:%d\n", byte);
ParodusInfo("Nanomsg client1 - Testing Upstream Registration msg send\n");
CU_ASSERT_EQUAL( byte, size );
//************************************************************
int sock1 = nn_socket (AF_SP, NN_PULL);
byte = 0;
bind = nn_bind(sock1, reg.u.reg.url);
CU_ASSERT(bind >= 0);
void *buf = NULL;
rc = nn_setsockopt(sock1, NN_SOL_SOCKET, NN_RCVTIMEO, &t, sizeof(t));
CU_ASSERT(rc >= 0);
ParodusPrint("Client 1 waiting for acknowledgement \n");
byte = nn_recv(sock1, &buf, NN_MSG, 0);
ParodusInfo("Data Received for client 1 : %s \n", (char * )buf);
rv1 = wrp_to_struct((void *)buf, byte, WRP_BYTES, &msg1);
CU_ASSERT_EQUAL( rv1, byte );
ParodusPrint("msg1->msg_type for client 1 = %d \n", msg1->msg_type);
ParodusPrint("msg1->status for client 1 = %d \n", msg1->u.auth.status);
CU_ASSERT_EQUAL(msg1->msg_type, 2);
CU_ASSERT_EQUAL(msg1->u.auth.status, 200);
rc = nn_freemsg(buf);
CU_ASSERT(rc == 0);
free(bytes);
wrp_free_struct(msg1);
rc = nn_shutdown(sock1, bind);
CU_ASSERT(rc == 0);
}
void test_nanomsg_client_registration2()
{
/*****Test svc registation for upstream - nanomsg client2 ***/
ParodusInfo("test_nanomsg_client_registration2\n");
const wrp_msg_t reg = { .msg_type = WRP_MSG_TYPE__SVC_REGISTRATION,
.u.reg.service_name = "iot2",
.u.reg.url = CLIENT2_URL};
void *bytes;
int size;
int rv1, rc;
wrp_msg_t *msg1;
int sock, bind;
int byte =0;
int t=28000;
// msgpack encode
ParodusPrint("msgpack encode\n");
size = wrp_struct_to( &reg, WRP_BYTES, &bytes );
/*** Enable this to decode and verify packed upstream registration msg **/
/**
rv = wrp_to_struct(bytes, size, WRP_BYTES, &message);
ParodusPrint("decoded msgType:%d\n", message->msg_type);
ParodusPrint("decoded service_name:%s\n", message->u.reg.service_name);
ParodusPrint("decoded dest:%s\n", message->u.reg.url);
wrp_free_struct(message);
***/
//nanomsg socket
sock = nn_socket (AF_SP, NN_PUSH);
int connect = nn_connect (sock, ENDPOINT);
CU_ASSERT( connect >= 0);
rc = nn_setsockopt(sock, NN_SOL_SOCKET, NN_SNDTIMEO, &t, sizeof(t));
CU_ASSERT(rc >= 0);
byte = nn_send (sock, bytes, size,0);
ParodusInfo("----->Expected byte to be sent:%d\n", size);
ParodusInfo("----->actual byte sent:%d\n", byte);
ParodusInfo("Nanomsg client2 - Testing Upstream Registration msg send\n");
CU_ASSERT_EQUAL( byte, size );
int sock1 = nn_socket (AF_SP, NN_PULL);
byte = 0;
bind = nn_bind(sock1, reg.u.reg.url);
CU_ASSERT(bind >= 0);
void *buf1 = NULL;
rc = nn_setsockopt(sock1, NN_SOL_SOCKET, NN_RCVTIMEO, &t, sizeof(t));
CU_ASSERT(rc >= 0);
ParodusPrint("Client 2 waiting for acknowledgement \n");
byte = nn_recv(sock1, &buf1, NN_MSG, 0);
ParodusInfo("Data Received : %s \n", (char * )buf1);
rv1 = wrp_to_struct((void *)buf1, byte, WRP_BYTES, &msg1);
CU_ASSERT_EQUAL( rv1, byte );
ParodusPrint("msg1->msg_type for client 2 = %d \n", msg1->msg_type);
ParodusPrint("msg1->status for client 2 = %d \n", msg1->u.auth.status);
CU_ASSERT_EQUAL(msg1->msg_type, 2);
CU_ASSERT_EQUAL(msg1->u.auth.status, 200);
rc = nn_freemsg(buf1);
CU_ASSERT(rc == 0);
free(bytes);
wrp_free_struct(msg1);
rc = nn_shutdown(sock1, bind);
CU_ASSERT(rc == 0);
}
void test_nanomsg_client_registration3()
{
/*****Test svc registation for upstream - nanomsg client2 ***/
ParodusInfo("test_nanomsg_client_registration3\n");
const wrp_msg_t reg = { .msg_type = WRP_MSG_TYPE__SVC_REGISTRATION,
.u.reg.service_name = "iot",
.u.reg.url = CLIENT3_URL};
void *bytes;
int size;
int rv1, rc;
wrp_msg_t *msg1;
int sock;
int byte =0;
int t=35000;
// msgpack encode
ParodusPrint("msgpack encode\n");
size = wrp_struct_to( &reg, WRP_BYTES, &bytes );
/*** Enable this to decode and verify packed upstream registration msg **/
/**
rv = wrp_to_struct(bytes, size, WRP_BYTES, &message);
ParodusPrint("decoded msgType:%d\n", message->msg_type);
ParodusPrint("decoded service_name:%s\n", message->u.reg.service_name);
ParodusPrint("decoded dest:%s\n", message->u.reg.url);
wrp_free_struct(message);
***/
//nanomsg socket
sock = nn_socket (AF_SP, NN_PUSH);
int connect = nn_connect (sock, ENDPOINT);
CU_ASSERT(connect >= 0);
rc = nn_setsockopt(sock, NN_SOL_SOCKET, NN_SNDTIMEO, &t, sizeof(t));
CU_ASSERT(rc >= 0);
byte = nn_send (sock, bytes, size,0);
ParodusInfo("----->Expected byte to be sent:%d\n", size);
ParodusInfo("----->actual byte sent:%d\n", byte);
ParodusInfo("Nanomsg client3 - Testing Upstream Registration msg send\n");
CU_ASSERT_EQUAL( byte, size );
int sock1 = nn_socket (AF_SP, NN_PULL);
byte = 0;
int bind = nn_bind(sock1, reg.u.reg.url);
CU_ASSERT(bind >= 0);
ParodusPrint("Need to close this bind %d \n", bind);
void *buf2 = NULL;
rc = nn_setsockopt(sock1, NN_SOL_SOCKET, NN_RCVTIMEO, &t, sizeof(t));
CU_ASSERT(rc >= 0);
ParodusPrint("Client 3 is waiting for acknowledgement \n");
byte = nn_recv(sock1, &buf2, NN_MSG, 0);
ParodusInfo("Data Received : %s \n", (char * )buf2);
rv1 = wrp_to_struct((void *)buf2, byte, WRP_BYTES, &msg1);
CU_ASSERT_EQUAL( rv1, byte );
ParodusPrint("msg1->msg_type for client 3 = %d \n", msg1->msg_type);
ParodusPrint("msg1->status for client 3 = %d \n", msg1->u.auth.status);
CU_ASSERT_EQUAL(msg1->msg_type, 2);
CU_ASSERT_EQUAL(msg1->u.auth.status, 200);
rc = nn_freemsg(buf2);
CU_ASSERT(rc == 0);
free(bytes);
wrp_free_struct(msg1);
rc = nn_shutdown(sock1, bind);
CU_ASSERT(rc == 0);
}
void test_nanomsg_downstream_success()
{
ParodusInfo("test_nanomsg_downstream_success\n");
int sock;
int bit=0, rc;
wrp_msg_t *message;
void *buf =NULL;
char* destVal = NULL;
// char dest[32] = {'\0'};
char *dest = NULL;
//char *temp_ptr;
int bind = -1;
const wrp_msg_t msg = { .msg_type = WRP_MSG_TYPE__SVC_REGISTRATION,
.u.reg.service_name = "iot",
.u.reg.url = CLIENT3_URL};
sock = nn_socket (AF_SP, NN_PULL);
while(bind == -1)
{
bind = nn_bind(sock, msg.u.reg.url);
sleep(3);
}
ParodusPrint("Bind returns = %d \n", bind);
ParodusPrint("***** Nanomsg client3 in Receiving mode in %s *****\n", msg.u.reg.url);
bit = nn_recv (sock, &buf, NN_MSG, 0);
ParodusInfo ("----->Received downstream request from server to client3 : \"%s\"\n", (char *)buf);
ParodusPrint("Received %d bytes\n", bit);
CU_ASSERT(bit >= 0);
//Decode and verify downstream request has received by correct registered client
wrp_to_struct(buf, bit, WRP_BYTES, &message);
destVal = message->u.req.dest;
dest = strtok(destVal , "/");
//temp_ptr = strtok(destVal , "/");
// ParodusPrint("temp_ptr = %s \n", temp_ptr);
strcpy(dest,strtok(NULL , "/"));
ParodusInfo("------>decoded dest:%s\n", dest);
CU_ASSERT_STRING_EQUAL( msg.u.reg.service_name, dest );
wrp_free_struct(message);
//To send nanomsg client response upstream
send_nanomsg_upstream(&buf, bit);
rc = nn_freemsg(buf);
CU_ASSERT(rc == 0);
rc = nn_shutdown(sock, bind);
CU_ASSERT(rc == 0);
//Need to wait for parodus to finish it's task.
sleep(10);
}
void test_nanomsg_downstream_failure()
{
int sock, bind, rc;
int bit =0;
char *buf =NULL;
ParodusError("test_nanomsg_downstream_failure\n");
sleep(60);
sock = nn_socket (AF_SP, NN_PULL);
bind = nn_bind (sock, CLIENT3_URL);
CU_ASSERT(bind >= 0);
ParodusPrint("***** Nanomsg client3 in Receiving mode *****\n");
bit = nn_recv (sock, &buf, NN_MSG, 0);
ParodusInfo ("Received downstream request from server for client3 : \"%s\"\n", buf);
CU_ASSERT(bit >= 0);
rc = nn_freemsg(buf);
CU_ASSERT(rc == 0);
rc = nn_shutdown(sock, bind);
CU_ASSERT(rc == 0);
}
void test_checkHostIp()
{
int ret;
ParodusPrint("**********************************Calling check_host_ip \n");
ret = checkHostIp("fabric.webpa.comcast.net");
ParodusPrint("------------------> Ret = %d \n", ret);
CU_ASSERT_EQUAL(ret, 0);
}
void test_sendMessage()
{
noPollConnOpts * opts;
noPollCtx *ctx = NULL;
noPollConn *conn = NULL;
ParodusPrint("**********************************Calling sendMessage \n");
const char * headerNames[HTTP_CUSTOM_HEADER_COUNT] = {"X-WebPA-Device-Name","X-WebPA-Device-Protocols","User-Agent", "X-WebPA-Convey"};
const char * headerValues[HTTP_CUSTOM_HEADER_COUNT];
headerValues[0] = "123567892366";
headerValues[1] = "wrp-0.11,getset-0.1";
headerValues[2] = "WebPA-1.6 (TG1682_DEV_master_2016000000sdy;TG1682/ARRISGroup,Inc.;)";
headerValues[3] = "zacbvfxcvglodfjdigjkdshuihgkvn";
int headerCount = HTTP_CUSTOM_HEADER_COUNT;
//ctx = nopoll_ctx_new();
opts = nopoll_conn_opts_new ();
nopoll_conn_opts_ssl_peer_verify (opts, nopoll_false);
nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_2);
conn = nopoll_conn_tls_new(ctx, opts, "fabric.webpa.comcast.net", "8080", NULL, "/api/v2/device", NULL, NULL, "eth0",
headerNames, headerValues, headerCount);
/*while(conn == NULL)
{
opts = nopoll_conn_opts_new ();
nopoll_conn_opts_ssl_peer_verify (opts, nopoll_false);
nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_2);
conn = nopoll_conn_tls_new(ctx, opts, "fabric.webpa.comcast.net", 8080, NULL, "/api/v2/device", NULL, NULL, "eth0",
headerNames, headerValues, headerCount);
}*/
ParodusPrint("Sending conn as %p \n", conn);
sendMessage(conn, "hello", 6);
}
void test_parseCommandLine()
{
int argc =11;
char * command[15]={'\0'};
command[0] = "parodus";
command[1] = "--hw-model=TG1682";
command[2] = "--hw-serial-number=Fer23u948590";
command[3] = "--hw-manufacturer=ARRISGroup,Inc.";
command[4] = "--hw-mac=123567892366";
command[5] = "--hw-last-reboot-reason=unknown";
command[6] = "--fw-name=TG1682_DEV_master_2016000000sdy";
command[7] = "--webpa-ping-time=180";
command[8] = "--webpa-inteface-used=eth0";
command[9] = "--webpa-url=fabric.webpa.comcast.net";
command[10] = "--webpa-backoff-max=0";
ParodusCfg parodusCfg;
memset(&parodusCfg,0,sizeof(parodusCfg));
ParodusPrint("call parseCommand\n");
parseCommandLine(argc,command,&parodusCfg);
ParodusPrint("parodusCfg.webpa_ping_timeout is %d\n", parodusCfg.webpa_ping_timeout);
ParodusPrint("parodusCfg.webpa_backoff_max is %d\n", parodusCfg.webpa_backoff_max);
CU_ASSERT_STRING_EQUAL( parodusCfg.hw_model, "TG1682");
CU_ASSERT_STRING_EQUAL( parodusCfg.hw_serial_number, "Fer23u948590");
CU_ASSERT_STRING_EQUAL( parodusCfg.hw_manufacturer, "ARRISGroup,Inc.");
CU_ASSERT_STRING_EQUAL( parodusCfg.hw_mac, "123567892366");
CU_ASSERT_STRING_EQUAL( parodusCfg.hw_last_reboot_reason, "unknown");
CU_ASSERT_STRING_EQUAL( parodusCfg.fw_name, "TG1682_DEV_master_2016000000sdy");
CU_ASSERT( parodusCfg.webpa_ping_timeout==180);
CU_ASSERT_STRING_EQUAL( parodusCfg.webpa_interface_used, "eth0");
CU_ASSERT_STRING_EQUAL( parodusCfg.webpa_url, "fabric.webpa.comcast.net");
CU_ASSERT( parodusCfg.webpa_backoff_max==0);
}
void test_loadParodusCfg()
{
ParodusPrint("Calling test_loadParodusCfg \n");
//ParodusCfg parodusCfg, tmpcfg;
ParodusCfg tmpcfg;
ParodusCfg *Cfg;
Cfg = (ParodusCfg*)malloc(sizeof(ParodusCfg));
strcpy(Cfg->hw_model, "TG1682");
strcpy(Cfg->hw_serial_number, "Fer23u948590");
strcpy(Cfg->hw_manufacturer , "ARRISGroup,Inc.");
strcpy(Cfg->hw_mac , "123567892366");
memset(&tmpcfg,0,sizeof(tmpcfg));
loadParodusCfg(Cfg,&tmpcfg);
ParodusInfo("tmpcfg.hw_model = %s, tmpcfg.hw_serial_number = %s, tmpcfg.hw_manufacturer = %s, tmpcfg.hw_mac = %s, \n", tmpcfg.hw_model,tmpcfg.hw_serial_number, tmpcfg.hw_manufacturer, tmpcfg.hw_mac);
CU_ASSERT_STRING_EQUAL( tmpcfg.hw_model, "TG1682");
CU_ASSERT_STRING_EQUAL( tmpcfg.hw_serial_number, "Fer23u948590");
CU_ASSERT_STRING_EQUAL( tmpcfg.hw_manufacturer, "ARRISGroup,Inc.");
CU_ASSERT_STRING_EQUAL( tmpcfg.hw_mac, "123567892366");
}
void add_suites( CU_pSuite *suite )
{
ParodusInfo("--------Start of Test Cases Execution ---------\n");
*suite = CU_add_suite( "tests", NULL, NULL );
CU_add_test( *suite, "Test 1", test_nanomsg_client_registration1 );
CU_add_test( *suite, "Test 2", test_nanomsg_client_registration2 );
CU_add_test( *suite, "Test 3", test_nanomsg_client_registration3 );
CU_add_test( *suite, "Test 4", test_nanomsg_downstream_success );
//CU_add_test( *suite, "Test 5", test_nanomsg_downstream_failure );
ParodusInfo("-------------Integration testing is completed-----------\n");
ParodusInfo("******************************************************************\n");
//sleep(10);
ParodusInfo("-------------Start of Unit Test Cases Execution---------\n");
CU_add_test( *suite, "UnitTest 1", test_parseCommandLine );
CU_add_test( *suite, "UnitTest 2", test_checkHostIp );
CU_add_test( *suite, "UnitTest 3", test_sendMessage );
CU_add_test( *suite, "UnitTest 4", test_loadParodusCfg );
}
/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
int main( void )
{
pid_t pid, pid1;
char value[512] = {'\0'};
char* data =NULL;
int status;
char commandUrl[255];
pid_t curl_pid;
char * command[] = {"parodus","--hw-model=TG1682", "--hw-serial-number=Fer23u948590","--hw-manufacturer=ARRISGroup,Inc.","--hw-mac=123567892366","--hw-last-reboot-reason=unknown","--fw-name=TG1682_DEV_master_2016000000sdy","--boot-time=10","--webpa-ping-time=180","--webpa-inteface-used=eth0","--webpa-url=fabric-cd.webpa.comcast.net","--webpa-backoff-max=9", NULL};
//int size = sizeof(command)/sizeof(command[0]);
//int i;
//ParodusInfo("commad: ");
//for(i=0;i<size-1;i++)
//ParodusInfo("command:%s",command);
ParodusInfo("Starting parodus process \n");
const char *s = getenv("WEBPA_AUTH_HEADER");
sprintf(commandUrl, "curl -i -H \"Authorization:Basic %s\" -H \"Accept: application/json\" -w %%{time_total} -k \"https://api-cd.webpa.comcast.net:8090/api/v2/device/mac:123567892366/iot?names=Device.DeviceInfo.Webpa.X_COMCAST-COM_SyncProtocolVersion\"", s);
ParodusPrint("---------------------->>>>Executing system(commandUrl)\n");
curl_pid = getpid();
ParodusPrint("child process execution with curl_pid:%d\n", curl_pid);
pid = fork();
if (pid == -1)
{
ParodusError("fork was unsuccessful for pid (errno=%d, %s)\n",errno, strerror(errno));
return -1;
}
else if (pid == 0)
{
int err;
ParodusPrint("child process created for parodus\n");
pid = getpid();
ParodusPrint("child process execution with pid:%d\n", pid);
err = execv("../src/parodus", command);
if(errno == 2)
{
err = execv("./src/parodus", command);
}
ParodusError("err is %d, errno is %d\n",err, errno);
}
else if (pid > 0)
{
int link[2];
sleep(5);
//Starting test suites execution in new thread
ParodusPrint("Creating new thread for test suite execution\n");
pthread_t testId;
int err1 = 0;
err1 = pthread_create(&testId,NULL,handle_testsuites,(void *)&pid);
if(err1 != 0)
ParodusError("Error creating test suite thread %s\n",strerror(err1));
else
ParodusPrint("test suite thread created successfully\n");
if (pipe(link)==-1)
{
ParodusError("Failed to create pipe\n");
}
else
ParodusPrint("Created pipe to read curl output\n");
pid1 = fork();
if (pid1 == -1)
{
ParodusError("fork was unsuccessful for pid1 (errno=%d, %s)\n",errno, strerror(errno));
return -1;
}
else if(pid1 == 0)
{
while(NULL == fopen("/tmp/parodus_ready", "r"))
{
sleep(5);
}
dup2 (link[1], STDOUT_FILENO);
close(link[0]);
close(link[1]);
sleep(40);
system(commandUrl);
ParodusInfo("\n----Executed first Curl request for downstream ------- \n");
}
else if(pid1 > 0)
{
//wait fro child process to finish and read from pipe
waitpid(pid1, &status, 0);
//reading from pipe
ParodusPrint("parent process...:%d\n", pid1);
close(link[1]);
int nbytes = read(link[0], value, sizeof(value));
ParodusPrint("Read %d \n", nbytes);
if ((data = strstr(value, "message:Success")) !=NULL)
{
ParodusInfo("curl success\n");
}
else
{
ParodusError("curl failure..\n");
}
while(1);
}
}
return 0;
}
void *handle_testsuites(void* pid)
{
unsigned rv = 1;
CU_pSuite suite = NULL;
pid_t pid_parodus = *((int *)pid);
ParodusPrint("Starting handle_testsuites thread\n");
sleep(25);
if( CUE_SUCCESS == CU_initialize_registry() )
{
add_suites( &suite );
if( NULL != suite )
{
CU_basic_set_mode( CU_BRM_VERBOSE );
CU_basic_run_tests();
ParodusPrint( "\n" );
CU_basic_show_failures( CU_get_failure_list() );
ParodusPrint( "\n\n" );
rv = CU_get_number_of_tests_failed();
}
CU_cleanup_registry();
}
kill(pid_parodus, SIGKILL);
ParodusInfo("parodus process with pid %d is stopped\n", pid_parodus);
if( 0 != rv )
{
_exit(-1);
}
_exit(0);
}
static void send_nanomsg_upstream(void **buf, int size)
{
/**** To send nanomsg response to server ****/
int rv;
void *bytes;
int resp_size;
int sock;
int byte;
wrp_msg_t *message;
ParodusInfo("Decoding downstream request received from server\n");
rv = wrp_to_struct(*buf, size, WRP_BYTES, &message);
ParodusPrint("after downstream req decode:%d\n", rv);
/**** Preparing Nanomsg client response ****/
wrp_msg_t resp_m;
resp_m.msg_type = WRP_MSG_TYPE__REQ;
ParodusPrint("resp_m.msg_type:%d\n", resp_m.msg_type);
resp_m.u.req.source = message->u.req.dest;
ParodusPrint("------resp_m.u.req.source is:%s\n", resp_m.u.req.source);
resp_m.u.req.dest = message->u.req.source;
ParodusPrint("------resp_m.u.req.dest is:%s\n", resp_m.u.req.dest);
resp_m.u.req.transaction_uuid = message->u.req.transaction_uuid;
ParodusPrint("------resp_m.u.req.transaction_uuid is:%s\n", resp_m.u.req.transaction_uuid);
resp_m.u.req.headers = NULL;
resp_m.u.req.payload = "{statusCode:200,message:Success}";
ParodusPrint("------resp_m.u.req.payload is:%s\n", (char *)resp_m.u.req.payload);
resp_m.u.req.payload_size = strlen(resp_m.u.req.payload);
resp_m.u.req.metadata = NULL;
resp_m.u.req.include_spans = false;
resp_m.u.req.spans.spans = NULL;
resp_m.u.req.spans.count = 0;
ParodusPrint("Encoding downstream response\n");
resp_size = wrp_struct_to( &resp_m, WRP_BYTES, &bytes );
/*** Enable this to verify downstream response by decoding ***/
/***
wrp_msg_t *message1;
rv = wrp_to_struct(bytes, resp_size, WRP_BYTES, &message1);
ParodusPrint("after downstream response decode:%d\n", rv);
ParodusPrint("downstream response decoded msgType:%d\n", message1->msg_type);
ParodusPrint("downstream response decoded source:%s\n", message1->u.req.source);
ParodusPrint("downstream response decoded dest:%s\n", message1->u.req.dest);
ParodusPrint("downstream response decoded transaction_uuid:%s\n", message1->u.req.transaction_uuid);
ParodusPrint("downstream response decoded payload:%s\n", (char*)message1->u.req.payload);
wrp_free_struct(message1);
***/
/**** Nanomsg client sending msgs ****/
sock = nn_socket (AF_SP, NN_PUSH);
int connect = nn_connect (sock, ENDPOINT);
CU_ASSERT(connect >= 0);
sleep(1);
ParodusInfo("nanomsg client sending response upstream\n");
byte = nn_send (sock, bytes, resp_size,0);
ParodusInfo("----->Expected byte to be sent:%d\n", resp_size);
ParodusInfo("----->actual byte sent:%d\n", byte);
CU_ASSERT(byte==resp_size );
wrp_free_struct(message);
free(bytes);
ParodusPrint("---- End of send_nanomsg_upstream ----\n");
}

View File

@@ -59,45 +59,45 @@ void test_set_global_conn()
set_global_conn(conn);
CU_ASSERT(conn == get_global_conn());
close_and_unref_connection(get_global_conn());
free(opts);
}
void test_set_parodus_cfg()
{
ParodusCfg *cfg;
ParodusCfg cfg;
cfg = (ParodusCfg*)malloc(sizeof(ParodusCfg));
strcpy(cfg->hw_model, "TG1682");
strcpy(cfg->hw_serial_number, "Fer23u948590");
strcpy(cfg->hw_manufacturer , "ARRISGroup,Inc.");
strcpy(cfg->hw_mac , "123567892366");
strcpy(cfg->hw_last_reboot_reason , "unknown");
strcpy(cfg->fw_name , "2.364s2");
strcpy(cfg->webpa_path_url , "/api/v2/device");
strcpy(cfg->webpa_url , "fabric-cd.webpa.comcast.net");
strcpy(cfg->webpa_interface_used , "eth0");
strcpy(cfg->webpa_protocol , "WebPA-1.6");
strcpy(cfg->webpa_uuid , "1234567-345456546");
cfg->secureFlag = 1;
cfg->boot_time = 423457;
cfg->webpa_ping_timeout = 30;
cfg->webpa_backoff_max = 255;
strcpy(cfg.hw_model, "TG1682");
strcpy(cfg.hw_serial_number, "Fer23u948590");
strcpy(cfg.hw_manufacturer , "ARRISGroup,Inc.");
strcpy(cfg.hw_mac , "123567892366");
strcpy(cfg.hw_last_reboot_reason , "unknown");
strcpy(cfg.fw_name , "2.364s2");
strcpy(cfg.webpa_path_url , "/api/v2/device");
strcpy(cfg.webpa_url , "fabric-cd.webpa.comcast.net");
strcpy(cfg.webpa_interface_used , "eth0");
strcpy(cfg.webpa_protocol , "WebPA-1.6");
strcpy(cfg.webpa_uuid , "1234567-345456546");
cfg.secureFlag = 1;
cfg.boot_time = 423457;
cfg.webpa_ping_timeout = 30;
cfg.webpa_backoff_max = 255;
set_parodus_cfg(cfg);
set_parodus_cfg(&cfg);
CU_ASSERT_STRING_EQUAL(cfg->hw_model, get_parodus_cfg()->hw_model);
CU_ASSERT_STRING_EQUAL(cfg->webpa_uuid, get_parodus_cfg()->webpa_uuid);
CU_ASSERT_STRING_EQUAL(cfg->hw_serial_number, get_parodus_cfg()->hw_serial_number);
CU_ASSERT_STRING_EQUAL(cfg->hw_manufacturer , get_parodus_cfg()->hw_manufacturer);
CU_ASSERT_STRING_EQUAL(cfg->hw_mac, get_parodus_cfg()->hw_mac);
CU_ASSERT_STRING_EQUAL(cfg->hw_last_reboot_reason,get_parodus_cfg()->hw_last_reboot_reason);
CU_ASSERT_STRING_EQUAL(cfg->fw_name,get_parodus_cfg()->fw_name);
CU_ASSERT_STRING_EQUAL(cfg->webpa_url, get_parodus_cfg()->webpa_url);
CU_ASSERT_STRING_EQUAL(cfg->webpa_interface_used , get_parodus_cfg()->webpa_interface_used);
CU_ASSERT_STRING_EQUAL(cfg->webpa_protocol, get_parodus_cfg()->webpa_protocol);
CU_ASSERT_EQUAL(cfg->boot_time, get_parodus_cfg()->boot_time);
CU_ASSERT_EQUAL(cfg->webpa_ping_timeout, get_parodus_cfg()->webpa_ping_timeout);
CU_ASSERT_EQUAL(cfg->webpa_backoff_max, get_parodus_cfg()->webpa_backoff_max);
CU_ASSERT_EQUAL(cfg->secureFlag, get_parodus_cfg()->secureFlag);
CU_ASSERT_STRING_EQUAL(cfg.hw_model, get_parodus_cfg()->hw_model);
CU_ASSERT_STRING_EQUAL(cfg.webpa_uuid, get_parodus_cfg()->webpa_uuid);
CU_ASSERT_STRING_EQUAL(cfg.hw_serial_number, get_parodus_cfg()->hw_serial_number);
CU_ASSERT_STRING_EQUAL(cfg.hw_manufacturer , get_parodus_cfg()->hw_manufacturer);
CU_ASSERT_STRING_EQUAL(cfg.hw_mac, get_parodus_cfg()->hw_mac);
CU_ASSERT_STRING_EQUAL(cfg.hw_last_reboot_reason,get_parodus_cfg()->hw_last_reboot_reason);
CU_ASSERT_STRING_EQUAL(cfg.fw_name,get_parodus_cfg()->fw_name);
CU_ASSERT_STRING_EQUAL(cfg.webpa_url, get_parodus_cfg()->webpa_url);
CU_ASSERT_STRING_EQUAL(cfg.webpa_interface_used , get_parodus_cfg()->webpa_interface_used);
CU_ASSERT_STRING_EQUAL(cfg.webpa_protocol, get_parodus_cfg()->webpa_protocol);
CU_ASSERT_EQUAL(cfg.boot_time, get_parodus_cfg()->boot_time);
CU_ASSERT_EQUAL(cfg.webpa_ping_timeout, get_parodus_cfg()->webpa_ping_timeout);
CU_ASSERT_EQUAL(cfg.webpa_backoff_max, get_parodus_cfg()->webpa_backoff_max);
CU_ASSERT_EQUAL(cfg.secureFlag, get_parodus_cfg()->secureFlag);
}
void test_getWebpaConveyHeader()
@@ -117,6 +117,7 @@ void test_getWebpaConveyHeader()
CU_ASSERT_STRING_EQUAL(get_parodus_cfg()->webpa_protocol, cJSON_GetObjectItem(payload, WEBPA_PROTOCOL)->valuestring);
CU_ASSERT_EQUAL((int)get_parodus_cfg()->boot_time, cJSON_GetObjectItem(payload, BOOT_TIME)->valueint);
cJSON_Delete(payload);
}
void test_createSecureConnection()
@@ -175,7 +176,7 @@ void test_WebpaConveyHeaderWithNullValues()
CU_ASSERT_PTR_NULL(cJSON_GetObjectItem(payload, FIRMWARE_NAME));
CU_ASSERT_PTR_NULL(cJSON_GetObjectItem(payload, WEBPA_INTERFACE));
free(cfg);
cJSON_Delete(payload);
}
void add_suites( CU_pSuite *suite )

View File

@@ -41,13 +41,13 @@ nopoll_bool nopoll_base64_encode(const char *content,int length,char *output, in
UNUSED(content); UNUSED(length); UNUSED(output_size);
strcpy(output, "AWYFUJHUDUDKJDDRDKUIIKORE\nSFJLIRRSHLOUTDESTDJJITTESLOIUHJGDRS\nGIUY&%WSJ");
function_called();
return (nopoll_bool)mock();
return (nopoll_bool)(intptr_t)mock();
}
char *get_global_reconnect_reason()
{
function_called();
return (char *)mock();
return (char *)(intptr_t)mock();
}
/*----------------------------------------------------------------------------*/
/* Tests */
@@ -74,7 +74,7 @@ void test_getWebpaConveyHeader()
cfg.webpa_backoff_max = 255;
set_parodus_cfg(&cfg);
will_return(get_global_reconnect_reason, "Ping-Miss");
will_return(get_global_reconnect_reason, (intptr_t)"Ping-Miss");
expect_function_call(get_global_reconnect_reason);
will_return(nopoll_base64_encode, nopoll_true);
@@ -88,7 +88,7 @@ void err_getWebpaConveyHeader()
memset(&cfg, 0, sizeof(ParodusCfg));
set_parodus_cfg(&cfg);
will_return(get_global_reconnect_reason, NULL);
will_return(get_global_reconnect_reason, (intptr_t)NULL);
expect_function_call(get_global_reconnect_reason);
will_return(nopoll_base64_encode, nopoll_false);
expect_function_call(nopoll_base64_encode);

View File

@@ -75,6 +75,7 @@ void test_client_addtolist()
}
wrp_free_struct(message);
free(bytes);
ParodusInfo("test_client_addtolist done..\n");
}
@@ -187,6 +188,7 @@ void test_addtolist_multiple_clients()
}
wrp_free_struct(message);
free(bytes);
ParodusInfo("test_addtolist_multiple_clients done..\n");
}

View File

@@ -126,7 +126,7 @@ void nopoll_ctx_unref(noPollCtx * ctx)
noPollConn *get_global_conn(void)
{
function_called();
return (noPollConn *) mock();
return (noPollConn *) (intptr_t)mock();
}
void set_global_conn(noPollConn *conn)
@@ -144,7 +144,7 @@ void StartThread(void *(*start_routine) (void *))
noPollCtx* nopoll_ctx_new(void)
{
function_called();
return (noPollCtx*) mock();
return (noPollCtx*) (intptr_t)mock();
}
void initKeypress()
{
@@ -163,7 +163,7 @@ void test_createSocketConnection()
close_retry = false;
expect_function_call(nopoll_thread_handlers);
will_return(nopoll_ctx_new, &ctx);
will_return(nopoll_ctx_new, (intptr_t)&ctx);
expect_function_call(nopoll_ctx_new);
expect_function_call(nopoll_log_set_handler);
will_return(createNopollConnection, nopoll_true);
@@ -176,13 +176,13 @@ void test_createSocketConnection()
expect_function_call(nopoll_loop_wait);
expect_function_call(set_global_reconnect_reason);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(set_global_conn);
will_return(createNopollConnection, nopoll_true);
expect_function_call(createNopollConnection);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(nopoll_ctx_unref);
@@ -198,7 +198,7 @@ void test_createSocketConnection1()
close_retry = true;
expect_function_call(nopoll_thread_handlers);
will_return(nopoll_ctx_new, &ctx);
will_return(nopoll_ctx_new, (intptr_t)&ctx);
expect_function_call(nopoll_ctx_new);
expect_function_call(nopoll_log_set_handler);
will_return(createNopollConnection, nopoll_true);
@@ -209,13 +209,13 @@ void test_createSocketConnection1()
will_return(nopoll_loop_wait, 1);
expect_function_call(nopoll_loop_wait);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(set_global_conn);
will_return(createNopollConnection, nopoll_true);
expect_function_call(createNopollConnection);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(nopoll_ctx_unref);
@@ -245,7 +245,7 @@ void test_createSocketConnection2()
close_retry = false;
expect_function_call(nopoll_thread_handlers);
will_return(nopoll_ctx_new, &ctx);
will_return(nopoll_ctx_new, (intptr_t)&ctx);
expect_function_call(nopoll_ctx_new);
expect_function_call(nopoll_log_set_handler);
will_return(createNopollConnection, nopoll_true);
@@ -263,13 +263,13 @@ void test_createSocketConnection2()
expect_function_calls(nopoll_loop_wait, 7);
expect_function_call(set_global_reconnect_reason);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(set_global_conn);
will_return(createNopollConnection, nopoll_true);
expect_function_call(createNopollConnection);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(nopoll_ctx_unref);
@@ -283,7 +283,7 @@ void err_createSocketConnection()
heartBeatTimer = 0;
expect_function_call(nopoll_thread_handlers);
will_return(nopoll_ctx_new, NULL);
will_return(nopoll_ctx_new, (intptr_t)NULL);
expect_function_call(nopoll_ctx_new);
expect_function_call(nopoll_log_set_handler);
will_return(createNopollConnection, nopoll_true);
@@ -294,13 +294,13 @@ void err_createSocketConnection()
will_return(nopoll_loop_wait, 1);
expect_function_call(nopoll_loop_wait);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(set_global_conn);
will_return(createNopollConnection, nopoll_true);
expect_function_call(createNopollConnection);
will_return(get_global_conn, NULL);
will_return(get_global_conn, (intptr_t)NULL);
expect_function_call(get_global_conn);
expect_function_call(close_and_unref_connection);
expect_function_call(nopoll_ctx_unref);

View File

@@ -46,9 +46,9 @@ noPollConn * nopoll_conn_tls_new (noPollCtx * ctx, noPollConnOpts * options, co
UNUSED(origin); UNUSED(outbound_interface); UNUSED(headerNames); UNUSED(headerValues); UNUSED(headerCount);
function_called();
check_expected(ctx);
check_expected(host_ip);
return (noPollConn *)mock();
check_expected((intptr_t)ctx);
check_expected((intptr_t)host_ip);
return (noPollConn *) (intptr_t)mock();
}
noPollConn * nopoll_conn_new (noPollCtx * ctx, const char * host_ip, const char * host_port, const char * host_name, const char * get_url, const char * protocols, const char * origin, const char * outbound_interface, const char * headerNames[], const char * headerValues[], const int headerCount)
@@ -57,9 +57,9 @@ noPollConn * nopoll_conn_new (noPollCtx * ctx, const char * host_ip, const char
UNUSED(outbound_interface); UNUSED(headerNames); UNUSED(headerValues); UNUSED(headerCount);
function_called();
check_expected(ctx);
check_expected(host_ip);
return (noPollConn *)mock();
check_expected((intptr_t)ctx);
check_expected((intptr_t)host_ip);
return (noPollConn *)(intptr_t)mock();
}
nopoll_bool nopoll_conn_is_ok (noPollConn * conn)
@@ -80,7 +80,7 @@ nopoll_bool nopoll_conn_wait_until_connection_ready (noPollConn * conn, int time
char* getWebpaConveyHeader()
{
function_called();
return (char*) mock();
return (char*) (intptr_t)mock();
}
int checkHostIp(char * serverIP)
@@ -140,7 +140,7 @@ char *strtok(char *str, const char *delim)
{
UNUSED(str); UNUSED(delim);
function_called();
return (char*) mock();
return (char*) (intptr_t)mock();
}
void setMessageHandlers()
@@ -163,11 +163,11 @@ void test_createSecureConnection()
set_parodus_cfg(cfg);
assert_non_null(ctx);
will_return(getWebpaConveyHeader, "WebPA-1.6 (TG1682)");
will_return(getWebpaConveyHeader, (intptr_t)"WebPA-1.6 (TG1682)");
expect_function_call(getWebpaConveyHeader);
expect_value(nopoll_conn_tls_new, ctx, ctx);
expect_string(nopoll_conn_tls_new, host_ip, "localhost");
will_return(nopoll_conn_tls_new, &gNPConn);
expect_value(nopoll_conn_tls_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_tls_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_tls_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_tls_new);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
@@ -177,6 +177,7 @@ void test_createSecureConnection()
int ret = createNopollConnection(ctx);
assert_int_equal(ret, nopoll_true);
free(cfg);
nopoll_ctx_unref (ctx);
}
void test_createConnection()
@@ -191,11 +192,11 @@ void test_createConnection()
strcpy(cfg->webpa_url , "localhost");
set_parodus_cfg(cfg);
assert_non_null(ctx);
will_return(getWebpaConveyHeader, "WebPA-1.6 (TG1682)");
will_return(getWebpaConveyHeader, (intptr_t)"WebPA-1.6 (TG1682)");
expect_function_call(getWebpaConveyHeader);
expect_value(nopoll_conn_new, ctx, ctx);
expect_string(nopoll_conn_new, host_ip, "localhost");
will_return(nopoll_conn_new, &gNPConn);
expect_value(nopoll_conn_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_new);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
@@ -205,6 +206,7 @@ void test_createConnection()
int ret = createNopollConnection(ctx);
assert_int_equal(ret, nopoll_true);
free(cfg);
nopoll_ctx_unref (ctx);
}
void test_createConnectionConnNull()
@@ -220,19 +222,19 @@ void test_createConnectionConnNull()
set_parodus_cfg(cfg);
assert_non_null(ctx);
will_return(getWebpaConveyHeader, "");
will_return(getWebpaConveyHeader, (intptr_t)"");
expect_function_call(getWebpaConveyHeader);
expect_value(nopoll_conn_tls_new, ctx, ctx);
expect_string(nopoll_conn_tls_new, host_ip, "localhost");
will_return(nopoll_conn_tls_new, NULL);
expect_value(nopoll_conn_tls_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_tls_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_tls_new, (intptr_t)NULL);
expect_function_call(nopoll_conn_tls_new);
will_return(checkHostIp, -2);
expect_function_call(checkHostIp);
expect_function_call(getCurrentTime);
expect_value(nopoll_conn_tls_new, ctx, ctx);
expect_string(nopoll_conn_tls_new, host_ip, "localhost");
will_return(nopoll_conn_tls_new, NULL);
expect_value(nopoll_conn_tls_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_tls_new,(intptr_t)host_ip, "localhost");
will_return(nopoll_conn_tls_new, (intptr_t)NULL);
expect_function_call(nopoll_conn_tls_new);
will_return(checkHostIp, -2);
expect_function_call(checkHostIp);
@@ -244,9 +246,9 @@ void test_createConnectionConnNull()
will_return(kill, 1);
expect_function_call(kill);
expect_value(nopoll_conn_tls_new, ctx, ctx);
expect_string(nopoll_conn_tls_new, host_ip, "localhost");
will_return(nopoll_conn_tls_new, &gNPConn);
expect_value(nopoll_conn_tls_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_tls_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_tls_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_tls_new);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
@@ -255,6 +257,7 @@ void test_createConnectionConnNull()
expect_function_call(setMessageHandlers);
createNopollConnection(ctx);
free(cfg);
nopoll_ctx_unref (ctx);
}
void test_createConnectionConnNotOk()
@@ -269,11 +272,11 @@ void test_createConnectionConnNotOk()
strcpy(cfg->webpa_url , "localhost");
set_parodus_cfg(cfg);
assert_non_null(ctx);
will_return(getWebpaConveyHeader, "WebPA-1.6 (TG1682)");
will_return(getWebpaConveyHeader, (intptr_t)"WebPA-1.6 (TG1682)");
expect_function_call(getWebpaConveyHeader);
expect_value(nopoll_conn_new, ctx, ctx);
expect_string(nopoll_conn_new, host_ip, "localhost");
will_return(nopoll_conn_new, &gNPConn);
expect_value(nopoll_conn_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_new);
will_return(nopoll_conn_is_ok, nopoll_false);
expect_function_call(nopoll_conn_is_ok);
@@ -282,9 +285,9 @@ void test_createConnectionConnNotOk()
expect_function_call(nopoll_conn_ref_count);
expect_function_call(nopoll_conn_unref);
expect_value(nopoll_conn_new, ctx, ctx);
expect_string(nopoll_conn_new, host_ip, "localhost");
will_return(nopoll_conn_new, &gNPConn);
expect_value(nopoll_conn_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_new);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
@@ -296,9 +299,9 @@ void test_createConnectionConnNotOk()
will_return(nopoll_conn_ref_count, 0);
expect_function_call(nopoll_conn_ref_count);
expect_value(nopoll_conn_new, ctx, ctx);
expect_string(nopoll_conn_new, host_ip, "localhost");
will_return(nopoll_conn_new, &gNPConn);
expect_value(nopoll_conn_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_new, (intptr_t)host_ip, "localhost");
will_return(nopoll_conn_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_new);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
@@ -306,19 +309,19 @@ void test_createConnectionConnNotOk()
expect_function_call(nopoll_conn_wait_until_connection_ready);
will_return(strncmp, 0);
expect_function_call(strncmp);
will_return(strtok, "");
will_return(strtok, "");
will_return(strtok, "p.10.0.0.12");
will_return(strtok, "8080");
will_return(strtok, (intptr_t)"");
will_return(strtok, (intptr_t)"");
will_return(strtok, (intptr_t)"p.10.0.0.12");
will_return(strtok, (intptr_t)"8080");
expect_function_calls(strtok, 4);
expect_function_call(nopoll_conn_close);
will_return(nopoll_conn_ref_count, 1);
expect_function_call(nopoll_conn_ref_count);
expect_function_call(nopoll_conn_unref);
expect_value(nopoll_conn_new, ctx, ctx);
expect_string(nopoll_conn_new, host_ip, "10.0.0.12");
will_return(nopoll_conn_new, &gNPConn);
expect_value(nopoll_conn_new, (intptr_t)ctx, (intptr_t)ctx);
expect_string(nopoll_conn_new, (intptr_t)host_ip, "10.0.0.12");
will_return(nopoll_conn_new, (intptr_t)&gNPConn);
expect_function_call(nopoll_conn_new);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
@@ -328,6 +331,7 @@ void test_createConnectionConnNotOk()
int ret = createNopollConnection(ctx);
assert_int_equal(ret, nopoll_true);
free(cfg);
nopoll_ctx_unref (ctx);
}
void err_createConnectionCtxNull()

View File

@@ -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 */
@@ -50,13 +50,7 @@ int get_numOfClients()
reg_list_item_t * get_global_node(void)
{
function_called();
return (reg_list_item_t *) mock();
}
ParodusCfg *get_parodus_cfg(void)
{
function_called();
return (ParodusCfg*) mock();
return mock_ptr_type(reg_list_item_t *);
}
ssize_t wrp_to_struct( const void *bytes, const size_t length,
@@ -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,15 +95,12 @@ 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(get_global_node, head);
will_return(validate_partner_id, 1);
expect_function_call(validate_partner_id);
will_return(get_global_node, (intptr_t)head);
expect_function_call(get_global_node);
will_return(nn_send, 20);
expect_function_calls(nn_send, 1);
@@ -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,9 +133,9 @@ 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(get_global_node, head);
will_return(validate_partner_id, 0);
expect_function_call(validate_partner_id);
will_return(get_global_node, (intptr_t)head);
expect_function_call(get_global_node);
will_return(nn_send, 20);
expect_function_calls(nn_send, 1);
@@ -164,16 +158,12 @@ 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(get_global_node, NULL);
will_return(validate_partner_id, 0);
expect_function_call(validate_partner_id);
will_return(get_global_node, (intptr_t)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);

View File

@@ -17,6 +17,9 @@
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <CUnit/Basic.h>
@@ -31,8 +34,17 @@ struct shared_data {
/*----------------------------------------------------------------------------*/
/* Mocks */
/*----------------------------------------------------------------------------*/
/* none */
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr)
{
UNUSED(attr); UNUSED(mutex);
return (int)mock();
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
UNUSED(mutex);
return (int)mock();
}
/*----------------------------------------------------------------------------*/
/* Tests */
/*----------------------------------------------------------------------------*/
@@ -72,6 +84,7 @@ void test_Mutex()
pthread_t thread[2];
data.number = 0;
will_return(pthread_mutex_init, 0);
data.mutex = createMutex();
pthread_create(&thread[0], NULL, a, (void*)(&data));
@@ -80,44 +93,40 @@ void test_Mutex()
pthread_join(thread[0], NULL);
pthread_join(thread[1], NULL);
will_return(pthread_mutex_destroy, 0);
destroyMutex(data.mutex);
CU_ASSERT(33 == data.number);
assert_int_equal(33, data.number);
}
void add_suites( CU_pSuite *suite )
void err_mutex()
{
ParodusInfo("--------Start of Test Cases Execution ---------\n");
*suite = CU_add_suite( "tests", NULL, NULL );
CU_add_test( *suite, "Test checkHostIp()", test_Mutex );
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
noPollPtr mutex = &mtx;
will_return(pthread_mutex_destroy, -1);
destroyMutex(mutex);
will_return(pthread_mutex_init, -1);
mutex = createMutex();
}
void err_mutexNull()
{
lockMutex(NULL);
unlockMutex(NULL);
destroyMutex(NULL);
}
/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
int main( void )
{
unsigned rv = 1;
CU_pSuite suite = NULL;
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_Mutex),
cmocka_unit_test(err_mutex),
cmocka_unit_test(err_mutexNull),
};
if( CUE_SUCCESS == CU_initialize_registry() ) {
add_suites( &suite );
if( NULL != suite ) {
CU_basic_set_mode( CU_BRM_VERBOSE );
CU_basic_run_tests();
ParodusPrint( "\n" );
CU_basic_show_failures( CU_get_failure_list() );
ParodusPrint( "\n\n" );
rv = CU_get_number_of_tests_failed();
}
CU_cleanup_registry();
}
return rv;
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -39,7 +39,7 @@ nopoll_bool nopoll_conn_is_ok( noPollConn *conn )
{
ParodusInfo("function_called : %s\n",__FUNCTION__);
function_called();
check_expected(conn);
check_expected((intptr_t)conn);
return (nopoll_bool)mock();
}
@@ -48,7 +48,7 @@ nopoll_bool nopoll_conn_is_ready( noPollConn *conn )
{
ParodusInfo("function_called : %s\n",__FUNCTION__);
function_called();
check_expected(conn);
check_expected((intptr_t)conn);
return (nopoll_bool)mock();
}
@@ -59,7 +59,7 @@ int __nopoll_conn_send_common (noPollConn * conn, const char * content, long le
ParodusInfo("function_called : %s\n",__FUNCTION__);
function_called();
check_expected(conn);
check_expected((intptr_t)conn);
check_expected(length);
return (int)mock();
}
@@ -71,7 +71,7 @@ int nopoll_conn_flush_writes(noPollConn * conn, long timeout, int previous_resul
ParodusInfo("function_called : %s\n",__FUNCTION__);
function_called();
check_expected(conn);
check_expected((intptr_t)conn);
check_expected(previous_result);
return (int)mock();
}
@@ -107,9 +107,9 @@ void test_setMessageHandlers()
void test_sendResponse()
{
int len = strlen("Hello Parodus!");
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, length, len);
will_return(__nopoll_conn_send_common, len);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length,(intptr_t) len);
will_return(__nopoll_conn_send_common, (intptr_t)len);
expect_function_calls(__nopoll_conn_send_common, 1);
int bytesWritten = sendResponse(conn, "Hello Parodus!", len);
@@ -121,13 +121,13 @@ void test_sendResponseWithFragments()
{
int len = (MAX_SEND_SIZE*2)+64;
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length, MAX_SEND_SIZE);
will_return(__nopoll_conn_send_common, MAX_SEND_SIZE);
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length, MAX_SEND_SIZE);
will_return(__nopoll_conn_send_common, MAX_SEND_SIZE);
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length, 64);
will_return(__nopoll_conn_send_common, 64);
expect_function_calls(__nopoll_conn_send_common, 3);
@@ -140,8 +140,8 @@ void err_sendResponse()
{
int len = strlen("Hello Parodus!");
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, length, len);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length, (intptr_t)len);
will_return(__nopoll_conn_send_common, -1);
expect_function_calls(__nopoll_conn_send_common, 1);
@@ -154,12 +154,12 @@ void err_sendResponseFlushWrites()
{
int len = strlen("Hello Parodus!");
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length, len);
will_return(__nopoll_conn_send_common, len-3);
expect_function_calls(__nopoll_conn_send_common, 1);
expect_value(nopoll_conn_flush_writes, conn, conn);
expect_value(nopoll_conn_flush_writes, (intptr_t)conn, (intptr_t)conn);
expect_value(nopoll_conn_flush_writes, previous_result, len-3);
will_return(nopoll_conn_flush_writes, len-3);
expect_function_calls(nopoll_conn_flush_writes, 1);
@@ -172,7 +172,7 @@ void err_sendResponseFlushWrites()
void err_sendResponseConnNull()
{
int len = strlen("Hello Parodus!");
expect_value(__nopoll_conn_send_common, conn, NULL);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)NULL);
expect_value(__nopoll_conn_send_common, length, len);
will_return(__nopoll_conn_send_common, -1);
expect_function_calls(__nopoll_conn_send_common, 1);
@@ -186,15 +186,15 @@ void test_sendMessage()
{
int len = strlen("Hello Parodus!");
expect_value(nopoll_conn_is_ok, conn, conn);
expect_value(nopoll_conn_is_ok, (intptr_t)conn, (intptr_t)conn);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
expect_value(nopoll_conn_is_ready, conn, conn);
expect_value(nopoll_conn_is_ready, (intptr_t)conn, (intptr_t)conn);
will_return(nopoll_conn_is_ready, nopoll_true);
expect_function_call(nopoll_conn_is_ready);
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, (intptr_t)conn, (intptr_t)conn);
expect_value(__nopoll_conn_send_common, length, len);
will_return(__nopoll_conn_send_common, len);
expect_function_calls(__nopoll_conn_send_common, 1);
@@ -206,20 +206,20 @@ void err_sendMessage()
{
int len = strlen("Hello Parodus!");
expect_value(nopoll_conn_is_ok, conn, conn);
expect_value(nopoll_conn_is_ok, (intptr_t)conn, (intptr_t)conn);
will_return(nopoll_conn_is_ok, nopoll_true);
expect_function_call(nopoll_conn_is_ok);
expect_value(nopoll_conn_is_ready, conn, conn);
expect_value(nopoll_conn_is_ready, (intptr_t)conn, (intptr_t)conn);
will_return(nopoll_conn_is_ready, nopoll_true);
expect_function_call(nopoll_conn_is_ready);
expect_value(__nopoll_conn_send_common, conn, conn);
expect_value(__nopoll_conn_send_common, (intptr_t)conn,(intptr_t) conn);
expect_value(__nopoll_conn_send_common, length, len);
will_return(__nopoll_conn_send_common, len-2);
expect_function_calls(__nopoll_conn_send_common, 1);
expect_value(nopoll_conn_flush_writes, conn, conn);
expect_value(nopoll_conn_flush_writes, (intptr_t)conn, (intptr_t)conn);
expect_value(nopoll_conn_flush_writes, previous_result, len-2);
will_return(nopoll_conn_flush_writes, len-3);
expect_function_calls(nopoll_conn_flush_writes, 1);
@@ -231,7 +231,7 @@ void err_sendMessageConnNull()
{
int len = strlen("Hello Parodus!");
expect_value(nopoll_conn_is_ok, conn, NULL);
expect_value(nopoll_conn_is_ok, (intptr_t)conn, (intptr_t)NULL);
will_return(nopoll_conn_is_ok, nopoll_false);
expect_function_call(nopoll_conn_is_ok);

230
tests/test_partners_check.c Executable file
View File

@@ -0,0 +1,230 @@
/**
* 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*) (intptr_t)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, (intptr_t)&cfg);
expect_function_call(get_parodus_cfg);
int ret = validate_partner_id(msg, NULL);
assert_int_equal(ret, 1);
free(msg);
}
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, (intptr_t)&cfg);
expect_function_call(get_parodus_cfg);
int ret = validate_partner_id(msg, NULL);
assert_int_equal(ret, 1);
free(msg);
}
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, (intptr_t)&cfg);
expect_function_call(get_parodus_cfg);
int ret = validate_partner_id(msg, NULL);
assert_int_equal(ret, 0);
free(msg);
}
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, (intptr_t)&cfg);
expect_function_call(get_parodus_cfg);
int ret = validate_partner_id(msg, NULL);
assert_int_equal(ret, -1);
free(msg);
}
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, (intptr_t)&cfg);
expect_function_call(get_parodus_cfg);
partners_t *list = NULL;
int ret = validate_partner_id(msg, &list);
assert_int_equal(ret, 1);
free(msg);
}
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, (intptr_t)&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");
int i;
for(i = 0; i< (int) list->count; i++)
{
free(list->partner_ids[i]);
}
free(list);
free(msg);
}
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, (intptr_t)&cfg);
expect_function_call(get_parodus_cfg);
int ret = validate_partner_id(msg, NULL);
assert_int_equal(ret, 0);
free(msg);
}
void test_validate_partner_id_for_event_withoutId()
{
partners_t *partner_ids = (partners_t *) malloc(sizeof(partners_t));
partner_ids->count = 1;
partner_ids->partner_ids[0] = (char *) malloc(sizeof(char)*64);
strcpy(partner_ids->partner_ids[0], "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, (intptr_t)&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");
int i;
for(i = 0; i< (int) list->count; i++)
{
free(list->partner_ids[i]);
}
free(list);
free(msg);
free(partner_ids);
}
/*----------------------------------------------------------------------------*/
/* 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);
}

View File

@@ -67,6 +67,8 @@ static void add_client()
ParodusPrint("addToList status is %d\n", status);
CU_ASSERT_EQUAL( status, 0 );
wrp_free_struct(message);
free(bytes);
}

View File

@@ -47,12 +47,12 @@ int get_numOfClients()
reg_list_item_t * get_global_node(void)
{
function_called();
return (reg_list_item_t *)mock();
return mock_ptr_type(reg_list_item_t *);
}
void listenerOnMessage(void * msg, size_t msgSize )
{
check_expected(msg);
check_expected((intptr_t)msg);
check_expected(msgSize);
function_called();
}
@@ -76,7 +76,7 @@ void test_messageHandlerTask()
numLoops = 1;
expect_value(listenerOnMessage, msg, ParodusMsgQ->payload);
expect_value(listenerOnMessage, (intptr_t)msg, (intptr_t)ParodusMsgQ->payload);
expect_value(listenerOnMessage, msgSize, ParodusMsgQ->len);
expect_function_call(listenerOnMessage);

View File

@@ -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 */
@@ -58,7 +59,7 @@ char *get_global_reconnect_reason()
reg_list_item_t * get_global_node(void)
{
function_called();
return (reg_list_item_t *)mock();
return mock_ptr_type(reg_list_item_t *);
}
int get_numOfClients()
@@ -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 */
/*----------------------------------------------------------------------------*/
@@ -226,6 +234,8 @@ void test_handle_upstream()
will_return(nn_recv, 12);
expect_function_call(nn_recv);
handle_upstream();
free(UpStreamMsgQ->next);
free(UpStreamMsgQ);
}
void err_handleUpstreamBindFailure()
@@ -263,7 +273,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);
@@ -274,6 +287,44 @@ void test_processUpstreamMessage()
processUpstreamMessage();
free(temp);
free(UpStreamMsgQ->next);
free(UpStreamMsgQ);
}
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);
free(UpStreamMsgQ->next);
free(UpStreamMsgQ);
}
void test_processUpstreamMessageRegMsg()
@@ -304,7 +355,7 @@ void test_processUpstreamMessageRegMsg()
will_return(get_numOfClients, 1);
expect_function_call(get_numOfClients);
will_return(get_global_node, head);
will_return(get_global_node, (intptr_t)head);
expect_function_call(get_global_node);
will_return(nn_shutdown, 1);
@@ -332,6 +383,8 @@ void test_processUpstreamMessageRegMsg()
processUpstreamMessage();
free(temp);
free(head);
free(UpStreamMsgQ->next);
free(UpStreamMsgQ);
}
void test_processUpstreamMessageRegMsgNoClients()
@@ -372,6 +425,8 @@ void test_processUpstreamMessageRegMsgNoClients()
processUpstreamMessage();
free(temp);
free(head);
free(UpStreamMsgQ->next);
free(UpStreamMsgQ);
}
void err_processUpstreamMessage()
@@ -402,6 +457,7 @@ void err_processUpstreamMessageDecodeErr()
expect_function_call(nn_freemsg);
processUpstreamMessage();
free(temp);
free(UpStreamMsgQ);
}
void err_processUpstreamMessageMetapackFailure()
@@ -425,6 +481,7 @@ void err_processUpstreamMessageMetapackFailure()
expect_function_call(nn_freemsg);
processUpstreamMessage();
free(temp);
free(UpStreamMsgQ);
}
void err_processUpstreamMessageRegMsg()
@@ -458,7 +515,7 @@ void err_processUpstreamMessageRegMsg()
will_return(get_numOfClients, 1);
expect_function_call(get_numOfClients);
will_return(get_global_node, head);
will_return(get_global_node, (intptr_t)head);
expect_function_call(get_global_node);
will_return(nn_shutdown, -1);
@@ -488,7 +545,10 @@ void err_processUpstreamMessageRegMsg()
processUpstreamMessage();
free(temp);
free(head->next);
free(head);
free(UpStreamMsgQ->next);
free(UpStreamMsgQ);
}
void test_sendUpstreamMsgToServer()
@@ -505,6 +565,7 @@ void test_sendUpstreamMsgToServer()
expect_function_call(appendEncodedData);
expect_function_call(sendMessage);
sendUpstreamMsgToServer(&bytes, 110);
free(bytes);
}
void err_sendUpstreamMsgToServer()
@@ -527,6 +588,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),