Compare commits

...

24 Commits

Author SHA1 Message Date
selvam krishnamoorthy
63e8b2baa0 Merge pull request #104 from selvamKrish/hostname-validation
Hostname validation
2017-06-12 15:06:07 +05:30
Selvam Krishnamoorthy
6907fc8d13 Add outbound interface to nopoll options 2017-04-26 11:18:52 +05:30
Selvam Krishnamoorthy
7b3625b5ed Handle Ipv6 and Ipv4 connection in parodus 2017-04-24 15:52:27 +05:30
Selvam Krishnamoorthy
b4c988c2ce Use ipv4 when ipv6 connection fails to connect to server 2017-04-17 15:00:19 +05:30
Selvam Krishnamoorthy
c6c44af756 Modify code to support lastest nopoll changes 2017-04-14 17:33:58 +05:30
Weston Schmidt
823877051f Merge pull request #87 from cableramki/master
Corrected libseshat module name.
2017-04-12 19:22:15 -07:00
Ramki Ananthakrishnan
613156c30e Corrected libseshat module name. 2017-04-12 17:20:06 -07:00
cableramki
d8b73af94d Merge pull request #86 from cableramki/master
Fixed broken Fedora (as approved for libparodus:- _https://github.com/Comcast/libparodus/pull/14_) & Travis builds.
2017-04-12 17:07:18 -07:00
Ramki Ananthakrishnan
9462a6ebc9 Added fix for Travis build. 2017-04-12 16:33:28 -07:00
Ramki Ananthakrishnan
fd0d86c111 Fixed broken Fedora build. 2017-04-12 16:19:27 -07:00
Weston Schmidt
90072a3ed1 Merge pull request #85 from sadhyama/master
To fix build issues on ubuntu 14.04 32 bit
2017-04-11 10:27:08 -07:00
Sadhyama
77d6af20b4 To fix build issues on ubuntu 14.04 32 bit 2017-04-11 20:50:23 +05:30
Weston Schmidt
2de416cf7b Merge pull request #84 from gbuddappagari/master
partner_id check
2017-04-11 00:33:09 -07:00
Gayathri
0ea486815d Added test for partners_check.c 2017-04-10 16:06:35 +05:30
Gayathri
7b08801146 Added partner_id check for wrp msg_type EVENT 2017-04-10 15:57:48 +05:30
Weston Schmidt
b2718d403f Merge pull request #82 from manderiasian/master
Adding seshat-url [e] to command line parameters.
2017-04-06 10:36:28 -07:00
mander207
f8f7b90720 Improving code, so next victim will not get SEGV when changing argc in only one place ;-) 2017-04-05 16:55:31 -07:00
mander207
76e4836193 Adding seshat-url [e] to command line parameters. 2017-04-05 16:15:42 -07:00
Weston Schmidt
16cc213451 Merge pull request #81 from gbuddappagari/master
To add partner_id to metadata map
2017-04-05 13:03:40 -07:00
Weston Schmidt
4da4b8d471 Merge pull request #80 from manderiasian/master
Adding libseshat to parodus. Initializing the seshat library.
2017-04-05 09:49:14 -07:00
Gayathri
c6add55913 Added partner_id to metadata 2017-04-05 15:06:59 +05:30
Gayathri
8386462528 Added negative test cases for mutex.c 2017-04-05 14:55:57 +05:30
mander207
b4336ba81f Reordering libs in link order 2017-04-04 21:50:50 -07:00
mander207
d87534cd5b Adding libseshat to parodus. Initializing the seshat library. 2017-04-04 21:25:45 -07:00
23 changed files with 752 additions and 1808 deletions

View File

@@ -21,7 +21,7 @@ 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 uuid-dev
script:
- mkdir build

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})
@@ -36,6 +36,7 @@ include_directories(${INCLUDE_DIR}
${INCLUDE_DIR}/wrp-c
${INCLUDE_DIR}/libparodus
${INCLUDE_DIR}/cimplog
${INCLUDE_DIR}/libseshat
)
# Compile options/flags
@@ -67,13 +68,12 @@ add_dependencies(libtrower-base64 trower-base64)
# nopoll external dependency
#-------------------------------------------------------------------------------
set(PATCHES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/patches)
set(NOPOLL_LOG_SRC ${PREFIX_DIR}/nopoll/src/nopoll/src/nopoll_log.c)
ExternalProject_Add(nopoll
PREFIX ${PREFIX_DIR}/nopoll
GIT_REPOSITORY https://github.com/ASPLes/nopoll.git
GIT_TAG "b18aacc06b4dc9700e0a261efc201a8e125e4328"
PATCH_COMMAND patch -p1 < ${PATCHES_DIR}/nopoll.patch
GIT_REPOSITORY https://github.com/selvamKrish/nopoll.git
GIT_TAG "testing-branch"
PATCH_COMMAND ""
COMMAND touch NEWS README AUTHORS ChangeLog
COMMAND libtoolize --force
COMMAND aclocal
@@ -166,6 +166,16 @@ ExternalProject_Add(libparodus
add_library(liblibparodus STATIC SHARED IMPORTED)
add_dependencies(liblibparodus libparodus)
# libseshat external dependency
#-------------------------------------------------------------------------------
ExternalProject_Add(libseshat
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/_prefix/libseshat
GIT_REPOSITORY https://github.com/comcast/seshat.git
CMAKE_ARGS += -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}
)
add_library(liblibseshat STATIC SHARED IMPORTED)
add_dependencies(liblibseshat libseshat)
if (BUILD_TESTING)
# cmocka external dependency
#-------------------------------------------------------------------------------
@@ -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)

File diff suppressed because it is too large Load Diff

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})
@@ -22,6 +22,8 @@ target_link_libraries (parodus
-lmsgpackc
-ltrower-base64
-lnopoll
-llibseshat
-luuid
-lm
-lcimplog
-lssl

View File

@@ -32,6 +32,7 @@ void set_parodus_cfg(ParodusCfg *cfg)
void parseCommandLine(int argc,char **argv,ParodusCfg * cfg)
{
int c;
while (1)
{
static struct option long_options[] = {
@@ -48,11 +49,12 @@ void parseCommandLine(int argc,char **argv,ParodusCfg * cfg)
{"webpa-inteface-used", required_argument, 0, 'i'},
{"parodus-local-url", required_argument, 0, 'l'},
{"partner-id", required_argument, 0, 'p'},
{"seshat-url", required_argument, 0, 'e'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "m:s:f:d:r:n:b:u:t:o:i:l:p:",long_options, &option_index);
c = getopt_long (argc, argv, "m:s:f:d:r:n:b:u:t:o:i:l:p:e:",long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
@@ -79,7 +81,12 @@ void parseCommandLine(int argc,char **argv,ParodusCfg * cfg)
parStrncpy(cfg->hw_mac, optarg,sizeof(cfg->hw_mac));
ParodusInfo("hw_mac is %s\n",cfg->hw_mac);
break;
case 'e':
parStrncpy(cfg->seshat_url, optarg,sizeof(cfg->seshat_url));
ParodusInfo("seshat_url is %s\n",cfg->seshat_url);
break;
case 'r':
parStrncpy(cfg->hw_last_reboot_reason, optarg,sizeof(cfg->hw_last_reboot_reason));
ParodusInfo("hw_last_reboot_reason is %s\n",cfg->hw_last_reboot_reason);

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"
@@ -60,6 +61,7 @@ typedef struct
unsigned int secureFlag;
char local_url[124];
char partner_id[64];
char seshat_url[128];
} ParodusCfg;
/*----------------------------------------------------------------------------*/

View File

@@ -17,6 +17,7 @@
#include "mutex.h"
#include "spin_thread.h"
#include "service_alive.h"
#include <libseshat.h>
/*----------------------------------------------------------------------------*/
/* Macros */
@@ -42,7 +43,8 @@ void createSocketConnection(void *config_in, void (* initKeypress)())
int intTimer=0;
ParodusCfg *tmpCfg = (ParodusCfg*)config_in;
noPollCtx *ctx;
bool seshat_started;
loadParodusCfg(tmpCfg,get_parodus_cfg());
ParodusPrint("Configure nopoll thread handlers in Parodus\n");
nopoll_thread_handlers(&createMutex, &destroyMutex, &lockMutex, &unlockMutex);
@@ -71,6 +73,15 @@ void createSocketConnection(void *config_in, void (* initKeypress)())
(* initKeypress) ();
}
/* Start seshat lib interface */
seshat_started = (0 == init_lib_seshat(get_parodus_cfg()->seshat_url));
if (false == seshat_started) {
ParodusPrint("init_lib_seshat() Failed, seshatlib not available!\n");
} else {
ParodusPrint("init_lib_seshat() seshatlib initialized! (url %s)\n",
get_parodus_cfg()->seshat_url);
}
do
{
nopoll_loop_wait(ctx, 5000000);
@@ -112,5 +123,10 @@ void createSocketConnection(void *config_in, void (* initKeypress)())
close_and_unref_connection(get_global_conn());
nopoll_ctx_unref(ctx);
nopoll_cleanup_library();
if (seshat_started) {
shutdown_seshat_lib();
}
}

View File

@@ -26,6 +26,8 @@
char deviceMAC[32]={'\0'};
static char *reconnect_reason = "webpa_process_starts";
static noPollConn *g_conn = NULL;
static noPollConnOpts * createConnOpts ();
static noPollConn * nopoll_tls_common_conn (noPollCtx * ctx,char * serverAddr,char *serverPort);
/*----------------------------------------------------------------------------*/
/* External Functions */
@@ -60,20 +62,10 @@ int createNopollConnection(noPollCtx *ctx)
bool initial_retry = false;
int backoffRetryTime = 0;
int max_retry_sleep;
char device_id[32]={'\0'};
char user_agent[512]={'\0'};
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];
int headerCount = HTTP_CUSTOM_HEADER_COUNT; /* Invalid X-Webpa-Convey header Bug # WEBPA-787 */
char port[8];
noPollConnOpts * opts;
char server_Address[256];
char redirectURL[128]={'\0'};
char *temp_ptr, *conveyHeader;
int connErr=0;
struct timespec connErr_start,connErr_end,*connErr_startPtr,*connErr_endPtr;
connErr_startPtr = &connErr_start;
connErr_endPtr = &connErr_end;
char *temp_ptr;
//Retry Backoff count shall start at c=2 & calculate 2^c - 1.
int c=2;
@@ -91,34 +83,9 @@ int createNopollConnection(noPollCtx *ctx)
fclose(fp);
}
parStrncpy(deviceMAC, get_parodus_cfg()->hw_mac,sizeof(deviceMAC));
snprintf(device_id, sizeof(device_id), "mac:%s", deviceMAC);
ParodusInfo("Device_id %s\n",device_id);
headerValues[0] = device_id;
headerValues[1] = "wrp-0.11,getset-0.1";
ParodusPrint("BootTime In sec: %d\n", get_parodus_cfg()->boot_time);
ParodusInfo("Received reconnect_reason as:%s\n", reconnect_reason);
snprintf(user_agent, sizeof(user_agent),
"%s (%s; %s/%s;)",
((0 != strlen(get_parodus_cfg()->webpa_protocol)) ? get_parodus_cfg()->webpa_protocol : "unknown"),
((0 != strlen(get_parodus_cfg()->fw_name)) ? get_parodus_cfg()->fw_name : "unknown"),
((0 != strlen(get_parodus_cfg()->hw_model)) ? get_parodus_cfg()->hw_model : "unknown"),
((0 != strlen(get_parodus_cfg()->hw_manufacturer)) ? get_parodus_cfg()->hw_manufacturer : "unknown"));
ParodusInfo("User-Agent: %s\n",user_agent);
headerValues[2] = user_agent;
conveyHeader = getWebpaConveyHeader();
if(strlen(conveyHeader) > 0)
{
headerValues[3] = conveyHeader;
}
else
{
headerValues[3] = "";
headerCount -= 1;
}
snprintf(port,sizeof(port),"%d",8080);
parStrncpy(server_Address, get_parodus_cfg()->webpa_url, sizeof(server_Address));
ParodusInfo("server_Address %s\n",server_Address);
@@ -139,20 +106,13 @@ int createNopollConnection(noPollCtx *ctx)
if(get_parodus_cfg()->secureFlag)
{
ParodusPrint("secure true\n");
/* disable verification */
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);
connection = nopoll_conn_tls_new(ctx, opts, server_Address, port, NULL,
get_parodus_cfg()->webpa_path_url, NULL, NULL, get_parodus_cfg()->webpa_interface_used,
headerNames, headerValues, headerCount);// WEBPA-787
connection = nopoll_tls_common_conn(ctx,server_Address, port);
}
else
{
ParodusPrint("secure false\n");
connection = nopoll_conn_new(ctx, server_Address, port, NULL,
get_parodus_cfg()->webpa_path_url, NULL, NULL, get_parodus_cfg()->webpa_interface_used,
headerNames, headerValues, headerCount);// WEBPA-787
get_parodus_cfg()->webpa_path_url, NULL, NULL);// WEBPA-787
}
set_global_conn(connection);
@@ -167,7 +127,6 @@ int createNopollConnection(noPollCtx *ctx)
close_and_unref_connection(get_global_conn());
set_global_conn(NULL);
initial_retry = true;
ParodusInfo("Waiting with backoffRetryTime %d seconds\n", backoffRetryTime);
sleep(backoffRetryTime);
continue;
@@ -220,32 +179,6 @@ int createNopollConnection(noPollCtx *ctx)
}
else
{
/* If the connect error is due to DNS resolving to 10.0.0.1 then start timer.
* Timeout after 15 minutes if the error repeats continuously and kill itself.
*/
if((checkHostIp(server_Address) == -2))
{
if(connErr == 0)
{
getCurrentTime(connErr_startPtr);
connErr = 1;
ParodusInfo("First connect error occurred, initialized the connect error timer\n");
}
else
{
getCurrentTime(connErr_endPtr);
ParodusPrint("checking timeout difference:%ld\n", timeValDiff(connErr_startPtr, connErr_endPtr));
if(timeValDiff(connErr_startPtr, connErr_endPtr) >= (15*60*1000))
{
ParodusError("WebPA unable to connect due to DNS resolving to 10.0.0.1 for over 15 minutes; crashing service.\n");
reconnect_reason = "Dns_Res_webpa_reconnect";
LastReasonStatus = true;
kill(getpid(),SIGTERM);
}
}
}
initial_retry = true;
ParodusInfo("Waiting with backoffRetryTime %d seconds\n", backoffRetryTime);
sleep(backoffRetryTime);
@@ -273,7 +206,6 @@ int createNopollConnection(noPollCtx *ctx)
ParodusPrint("createNopollConnection(): close_mut unlock\n");
heartBeatTimer = 0;
// Reset connErr flag on successful connection
connErr = 0;
reconnect_reason = "webpa_process_starts";
LastReasonStatus =false;
ParodusPrint("LastReasonStatus reset after successful connection\n");
@@ -281,6 +213,54 @@ int createNopollConnection(noPollCtx *ctx)
return nopoll_true;
}
static noPollConn * nopoll_tls_common_conn (noPollCtx * ctx,char * serverAddr,char *serverPort)
{
noPollConnOpts * opts;
noPollConn *connection = NULL;
opts = createConnOpts();
ParodusPrint("Trying connection with Ipv6 \n");
connection = nopoll_conn_tls_new6 (ctx, opts,serverAddr,serverPort,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL);
if(connection == NULL)
{
ParodusPrint("Ipv6 connection failed. Trying connection with Ipv4 \n");
opts = createConnOpts();
connection = nopoll_conn_tls_new (ctx, opts,serverAddr,serverPort,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL);
}
return connection;
}
static noPollConnOpts * createConnOpts ()
{
noPollConnOpts * opts;
char device_id[32]={'\0'};
char * extra_headers, *conveyHeader = NULL;
char user_agent[512]={'\0'};
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);
nopoll_conn_opts_set_interface (opts,get_parodus_cfg()->webpa_interface_used);
snprintf(user_agent, sizeof(user_agent),"%s (%s; %s/%s;)",
((0 != strlen(get_parodus_cfg()->webpa_protocol)) ? get_parodus_cfg()->webpa_protocol : "unknown"),
((0 != strlen(get_parodus_cfg()->fw_name)) ? get_parodus_cfg()->fw_name : "unknown"),
((0 != strlen(get_parodus_cfg()->hw_model)) ? get_parodus_cfg()->hw_model : "unknown"),
((0 != strlen(get_parodus_cfg()->hw_manufacturer)) ? get_parodus_cfg()->hw_manufacturer : "unknown"));
ParodusInfo("User-Agent: %s\n",user_agent);
conveyHeader = getWebpaConveyHeader();
parStrncpy(deviceMAC, get_parodus_cfg()->hw_mac,sizeof(deviceMAC));
snprintf(device_id, sizeof(device_id), "mac:%s", deviceMAC);
ParodusInfo("Device_id %s\n",device_id);
extra_headers = nopoll_strdup_printf("\r\nX-WebPA-Device-Name: %s"
"\r\nX-WebPA-Device-Protocols: wrp-0.11,getset-0.1"
"\r\nUser-Agent: %s" "\r\nX-WebPA-Convey: %s",device_id,user_agent,(strlen(conveyHeader) > 0)? conveyHeader :"");
nopoll_conn_opts_set_extra_headers (opts,extra_headers);
return opts;
}
void close_and_unref_connection(noPollConn *conn)
{

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;
@@ -59,35 +57,15 @@ void listenerOnMessage(void * msg, size_t msgSize)
if(message->msg_type == WRP_MSG_TYPE__REQ)
{
ParodusPrint("numOfClients registered is %d\n", get_numOfClients());
if(message->u.req.partner_ids !=NULL)
int ret = validate_partner_id(message, NULL);
if(ret < 0)
{
ParodusPrint("Validating partner_id\n");
checkPartnerId = 1;
partnerId = get_parodus_cfg()->partner_id;
ParodusPrint("partnerId: %s\n",partnerId);
ParodusPrint("partner_ids count is %lu\n",message->u.req.partner_ids->count);
for(i = 0; i < (int) message->u.req.partner_ids->count; i++)
{
if(strcmp(partnerId, message->u.req.partner_ids->partner_ids[i]) == 0)
{
ParodusInfo("partner_id match found\n");
matchFlag = 1;
break;
}
}
if(matchFlag == 0)
{
response = cJSON_CreateObject();
cJSON_AddNumberToObject(response, "statusCode", 430);
cJSON_AddStringToObject(response, "message", "Invalid partner_id");
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 +98,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 +127,8 @@ void listenerOnMessage(void * msg, size_t msgSize)
sendUpstreamMsgToServer(&resp_bytes, size);
}
free(str);
free(resp_msg);
}
free(resp_msg);
}
}
}

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

@@ -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,40 @@ void *processUpstreamMessage()
}
}
}
else if(msgType == WRP_MSG_TYPE__EVENT)
{
ParodusInfo(" Received upstream event data\n");
partners_t *partnersList = NULL;
int ret = validate_partner_id(msg, &partnersList);
if(ret == 1)
{
wrp_msg_t *eventMsg = (wrp_msg_t *) malloc(sizeof(wrp_msg_t));
eventMsg->msg_type = msgType;
eventMsg->u.event.content_type=msg->u.event.content_type;
eventMsg->u.event.source=msg->u.event.source;
eventMsg->u.event.dest=msg->u.event.dest;
eventMsg->u.event.payload=msg->u.event.payload;
eventMsg->u.event.payload_size=msg->u.event.payload_size;
eventMsg->u.event.headers=msg->u.event.headers;
eventMsg->u.event.metadata=msg->u.event.metadata;
eventMsg->u.event.partner_ids = partnersList;
int size = wrp_struct_to( eventMsg, WRP_BYTES, &bytes );
if(size > 0)
{
sendUpstreamMsgToServer(&bytes, size);
}
free(eventMsg);
}
else
{
sendUpstreamMsgToServer(&message->msg, message->len);
}
}
else
{
//Sending to server for msgTypes 3, 4, 5, 6, 7, 8.
//Sending to server for msgTypes 3, 5, 6, 7, 8.
ParodusInfo(" Received upstream data with MsgType: %d\n", msgType);
//Appending metadata with packed msg received from client
if(metaPackSize > 0)
@@ -323,4 +354,3 @@ void sendUpstreamMsgToServer(void **resp_bytes, size_t resp_size)
}
}

View File

@@ -17,7 +17,7 @@ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DTEST ")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -W -g -fprofile-arcs -ftest-coverage -O0")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -O0")
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)
set (PARODUS_COMMON_LIBS gcov -lcunit -lcimplog -llibseshat -lwrp-c -luuid -lpthread -lm -lmsgpackc -lcjson -ltrower-base64 -lnopoll -lnanomsg -Wl,--no-as-needed -lrt)
link_directories ( ${LIBRARY_DIR} )
@@ -26,7 +26,7 @@ link_directories ( ${LIBRARY_DIR} )
#-------------------------------------------------------------------------------
add_test(NAME test_mutex COMMAND 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
@@ -51,6 +51,8 @@ target_link_libraries (libpd_test
cunit
-llibparodus
-lwrp-c
-llibseshat
-luuid
-lmsgpackc
-ltrower-base64
-lnanomsg
@@ -112,14 +114,14 @@ target_link_libraries (test_createConnection ${PARODUS_COMMON_LIBS} -lcmocka)
# test_client_list
#-------------------------------------------------------------------------------
add_test(NAME test_client_list COMMAND test_client_list)
add_executable(test_client_list test_client_list.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c ${PARODUS_COMMON_SRC})
add_executable(test_client_list test_client_list.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS})
#-------------------------------------------------------------------------------
# test_service_alive
#-------------------------------------------------------------------------------
add_test(NAME test_service_alive COMMAND test_service_alive)
add_executable(test_service_alive test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ${PARODUS_COMMON_SRC})
add_executable(test_service_alive test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS})
#-------------------------------------------------------------------------------
@@ -164,6 +166,13 @@ add_test(NAME test_ParodusInternal COMMAND test_ParodusInternal)
add_executable(test_ParodusInternal test_ParodusInternal.c ../src/ParodusInternal.c ../src/config.c ../src/string_helpers.c)
target_link_libraries (test_ParodusInternal -lcmocka ${PARODUS_COMMON_LIBS} )
#-------------------------------------------------------------------------------
# test_partners_check
#-------------------------------------------------------------------------------
add_test(NAME test_partners_check COMMAND test_partners_check)
add_executable(test_partners_check test_partners_check.c ../src/partners_check.c)
target_link_libraries (test_partners_check -lcmocka -lwrp-c -llibseshat ${PARODUS_COMMON_LIBS})
if (INTEGRATION_TESTING)
#-------------------------------------------------------------------------------
# simple_connection test
@@ -180,6 +189,7 @@ add_executable(simple_connection simple_connection.c ${PARODUS_COMMON_SRC}
../src/connection.c
../src/ParodusInternal.c
../src/client_list.c
../src/partners_check.c
../src/service_alive.c)
target_link_libraries (simple_connection ${PARODUS_COMMON_LIBS})
@@ -203,6 +213,7 @@ add_executable(simple simple.c
../src/ParodusInternal.c
../src/spin_thread.c
../src/client_list.c
../src/partners_check.c
../src/service_alive.c)
target_link_libraries (simple ${PARODUS_COMMON_LIBS} gcov -lnopoll -lnanomsg )

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

@@ -26,6 +26,8 @@
#include "../src/config.h"
#define K_argc 15
/*----------------------------------------------------------------------------*/
/* Mocks */
/*----------------------------------------------------------------------------*/
@@ -50,11 +52,12 @@ void test_setParodusConfig()
strcpy(cfg.webpa_protocol , "WebPA-1.6");
strcpy(cfg.webpa_uuid , "1234567-345456546");
strcpy(cfg.partner_id , "comcast");
strcpy(cfg.seshat_url, "ipc://tmp/seshat_service.url");
cfg.secureFlag = 1;
cfg.boot_time = 423457;
cfg.webpa_ping_timeout = 30;
cfg.webpa_backoff_max = 255;
set_parodus_cfg(&cfg);
ParodusCfg *temp = get_parodus_cfg();
@@ -70,6 +73,8 @@ void test_setParodusConfig()
assert_string_equal(cfg.webpa_protocol, temp->webpa_protocol);
assert_string_equal(cfg.webpa_uuid, temp->webpa_uuid);
assert_string_equal(cfg.partner_id, temp->partner_id);
assert_string_equal(cfg.seshat_url, temp->seshat_url);
assert_int_equal((int) cfg.secureFlag, (int) temp->secureFlag);
assert_int_equal((int) cfg.boot_time, (int) temp->boot_time);
@@ -92,23 +97,26 @@ void test_getParodusConfig()
void test_parseCommandLine()
{
int argc =14;
char * command[15]={'\0'};
int argc =K_argc;
char * command[argc+1];
int i = 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=br0";
command[9] = "--webpa-url=localhost";
command[10] = "--webpa-backoff-max=0";
command[11] = "--boot-time=1234";
command[12] = "--parodus-local-url=tcp://127.0.0.1:6666";
command[13] = "--partner-id=cox";
command[i++] = "parodus";
command[i++] = "--hw-model=TG1682";
command[i++] = "--hw-serial-number=Fer23u948590";
command[i++] = "--hw-manufacturer=ARRISGroup,Inc.";
command[i++] = "--hw-mac=123567892366";
command[i++] = "--hw-last-reboot-reason=unknown";
command[i++] = "--fw-name=TG1682_DEV_master_2016000000sdy";
command[i++] = "--webpa-ping-time=180";
command[i++] = "--webpa-inteface-used=br0";
command[i++] = "--webpa-url=localhost";
command[i++] = "--webpa-backoff-max=0";
command[i++] = "--boot-time=1234";
command[i++] = "--parodus-local-url=tcp://127.0.0.1:6666";
command[i++] = "--partner-id=cox";
command[i++] = "--seshat-url=ipc://127.0.0.1:7777";
command[i] = '\0';
ParodusCfg parodusCfg;
memset(&parodusCfg,0,sizeof(parodusCfg));
@@ -128,6 +136,8 @@ void test_parseCommandLine()
assert_int_equal( (int) parodusCfg.boot_time,1234);
assert_string_equal( parodusCfg.local_url,"tcp://127.0.0.1:6666");
assert_string_equal( parodusCfg.partner_id,"cox");
assert_string_equal( parodusCfg.seshat_url, "ipc://127.0.0.1:7777");
}
void test_parseCommandLineNull()
@@ -137,7 +147,7 @@ void test_parseCommandLineNull()
void err_parseCommandLine()
{
int argc =14;
int argc =K_argc;
char * command[20]={'\0'};
command[0] = "parodus";

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

@@ -40,26 +40,25 @@ pthread_mutex_t close_mut;
/*----------------------------------------------------------------------------*/
/* Mocks */
/*----------------------------------------------------------------------------*/
noPollConn * nopoll_conn_tls_new (noPollCtx * ctx, noPollConnOpts * options, 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)
noPollConn * nopoll_conn_tls_new (noPollCtx * ctx, noPollConnOpts * options, const char * host_ip, const char * host_port, const char * host_name, const char * get_url, const char * protocols, const char * origin)
{
UNUSED(options); UNUSED(host_port); UNUSED(host_name); UNUSED(get_url); UNUSED(protocols);
UNUSED(origin); UNUSED(outbound_interface); UNUSED(headerNames); UNUSED(headerValues); UNUSED(headerCount);
UNUSED(origin);
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)
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)
{
UNUSED(host_port); UNUSED(host_name); UNUSED(get_url); UNUSED(protocols); 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();
}
nopoll_bool nopoll_conn_is_ok (noPollConn * conn)
@@ -80,7 +79,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 +139,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 +162,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);
@@ -191,11 +190,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);
@@ -220,19 +219,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 +243,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);
@@ -269,11 +268,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 +281,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 +295,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 +305,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);

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,41 @@ 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 );
noPollPtr mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
will_return(pthread_mutex_destroy, -1);
destroyMutex(mutex);
will_return(pthread_mutex_init, -1);
mutex = createMutex();
free(mutex);
}
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);

205
tests/test_partners_check.c Executable file
View File

@@ -0,0 +1,205 @@
/**
* Copyright 2010-2016 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <malloc.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "../src/config.h"
#include "../src/partners_check.h"
#include "../src/ParodusInternal.h"
/*----------------------------------------------------------------------------*/
/* File Scoped Variables */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Mocks */
/*----------------------------------------------------------------------------*/
ParodusCfg *get_parodus_cfg(void)
{
function_called();
return (ParodusCfg*) (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);
}
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);
}
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);
}
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);
}
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);
}
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");
}
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);
}
void test_validate_partner_id_for_event_withoutId()
{
static partners_t partner_ids = {1,{"shaw"}};
wrp_msg_t *msg = (wrp_msg_t*) malloc(sizeof(wrp_msg_t));
memset(msg, 0, sizeof(wrp_msg_t));
msg->msg_type = WRP_MSG_TYPE__EVENT;
msg->u.event.partner_ids = &partner_ids;
ParodusCfg cfg;
memset(&cfg, 0, sizeof(ParodusCfg));
strcpy(cfg.partner_id, "comcast");
will_return(get_parodus_cfg, (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");
}
/*----------------------------------------------------------------------------*/
/* 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

@@ -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 */
/*----------------------------------------------------------------------------*/
@@ -263,7 +271,10 @@ void test_processUpstreamMessage()
will_return(wrp_to_struct, 12);
expect_function_call(wrp_to_struct);
will_return(appendEncodedData, 12);
will_return(validate_partner_id, 1);
expect_function_call(validate_partner_id);
will_return(appendEncodedData, 100);
expect_function_call(appendEncodedData);
expect_function_call(sendMessage);
@@ -276,6 +287,40 @@ void test_processUpstreamMessage()
free(temp);
}
void test_processUpstreamMessageInvalidPartner()
{
numLoops = 1;
metaPackSize = 20;
UpStreamMsgQ = (UpStreamMsg *) malloc(sizeof(UpStreamMsg));
UpStreamMsgQ->msg = "First Message";
UpStreamMsgQ->len = 13;
UpStreamMsgQ->next = (UpStreamMsg *) malloc(sizeof(UpStreamMsg));
UpStreamMsgQ->next->msg = "Second Message";
UpStreamMsgQ->next->len = 15;
UpStreamMsgQ->next->next = NULL;
temp = (wrp_msg_t *) malloc(sizeof(wrp_msg_t));
memset(temp,0,sizeof(wrp_msg_t));
temp->msg_type = 4;
will_return(wrp_to_struct, 12);
expect_function_call(wrp_to_struct);
will_return(validate_partner_id, 0);
expect_function_call(validate_partner_id);
will_return(appendEncodedData, 100);
expect_function_call(appendEncodedData);
expect_function_call(sendMessage);
expect_function_call(wrp_free_struct);
will_return(nn_freemsg,1);
expect_function_call(nn_freemsg);
processUpstreamMessage();
free(temp);
}
void test_processUpstreamMessageRegMsg()
{
numLoops = 1;
@@ -304,7 +349,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);
@@ -458,7 +503,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);
@@ -527,6 +572,7 @@ int main(void)
cmocka_unit_test(err_handleUpstreamBindFailure),
cmocka_unit_test(err_handleUpstreamSockFailure),
cmocka_unit_test(test_processUpstreamMessage),
cmocka_unit_test(test_processUpstreamMessageInvalidPartner),
cmocka_unit_test(test_processUpstreamMessageRegMsg),
cmocka_unit_test(test_processUpstreamMessageRegMsgNoClients),
cmocka_unit_test(err_processUpstreamMessage),