Skip to content

Commit

Permalink
Merge pull request rsyslog#144 from rgerhards/chk-errreporting
Browse files Browse the repository at this point in the history
testbench: check functionality for pUsr handling
  • Loading branch information
rgerhards authored Nov 20, 2018
2 parents 26f9e8d + 036c918 commit 3e91258
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 5 deletions.
3 changes: 3 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ TESTS= basic.sh \
oversize-msg-abort-errmsg.sh \
oversize-msg-accept-errmsg.sh \
truncate-oversize-msg.sh \
send-noconnect.sh \
receive-emptyconnect.sh \
selftest_receive_usage.sh
# OpenSSL tests only!
if ENABLE_TLS_OPENSSL
Expand All @@ -43,6 +45,7 @@ endif

EXTRA_DIST=$(TESTS) \
$(VALGRIND_TESTS) \
dummyclient.py \
test-framework.sh \
receive.c \
send.c \
Expand Down
10 changes: 10 additions & 0 deletions tests/dummyclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python

import socket
import os

port = int(os.environ['TESTPORT'])
print "dummyclient info: opening and closing port " + str(port) + " without sending data"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", port))
s.close()
12 changes: 12 additions & 0 deletions tests/receive-emptyconnect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# checks if server handles connection request by clients which are immediately
# closed (without sending data) validly. This also means an error message must
# be emitted. This situation can frequently happen in Proxy configurations.
# written 2018-11-20 by Rainer Gerhards, released under ASL 2.0
. ${srcdir:=$(pwd)}/test-framework.sh
startup_receiver $OPT_VERBOSE &> $OUTFILE
${srcdir}/dummyclient.py
stop_receiver
# TODO: we should word the error message clearer, then also change here
check_output "server closed relp session"
terminate
26 changes: 24 additions & 2 deletions tests/receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ static FILE *outFile = NULL;

static relpEngine_t *pRelpEngine;

#define USR_MAGIC 0x1234FFee
struct usrdata { /* used for testing user pointer pass-back */
int magic;
char *progname;
};
struct usrdata *userdata = NULL;
static void
hdlr_enable(int sig, void (*hdlr)())
{
Expand Down Expand Up @@ -92,9 +98,17 @@ void print_usage(void)
}

static void
onErr( __attribute__((unused)) void *pUsr, char *objinfo, char* errmesg, __attribute__((unused)) relpRetVal errcode)
onErr(void *pUsr, char *objinfo, char* errmesg, __attribute__((unused)) relpRetVal errcode)
{
fprintf(stderr, "receive: error '%s', object '%s'\n", errmesg, objinfo);
struct usrdata *pThis = (struct usrdata*) pUsr;
if(pUsr != userdata) {
fprintf(stderr, "receive: pUsr NOT pointing to usrdata!\n");
}
if(pThis->magic != USR_MAGIC) {
fprintf(stderr, "receive: pUsr magic incorrect in onErr, magic %8.8x "
"pUsr %p\n", pThis->magic, (void*) pThis);
}
fprintf(stderr, "%s: error '%s', object '%s'\n", pThis->progname, errmesg, objinfo);
if(errFile != NULL) {
fprintf(errFile, "receive: error '%s', object '%s'\n", errmesg, objinfo);
}
Expand Down Expand Up @@ -122,6 +136,10 @@ onAuthErr( __attribute__((unused)) void *pUsr, char *authinfo,
static void
exit_hdlr(void)
{
if(userdata != NULL) {
free(userdata->progname);
free(userdata);
}
if(errFile != NULL) {
fclose(errFile);
}
Expand Down Expand Up @@ -285,6 +303,10 @@ int main(int argc, char *argv[]) {

TRY(relpEngineListnerConstruct(pRelpEngine, &pRelpSrv));
TRY(relpSrvSetLstnPort(pRelpSrv, port));
userdata = calloc(1, sizeof(struct usrdata));
userdata->magic = USR_MAGIC;
userdata->progname = strdup("receive");
relpSrvSetUsrPtr(pRelpSrv, userdata);
if(maxDataSize != 0) {
TRY(relpSrvSetMaxDataSize(pRelpSrv, maxDataSize));
}
Expand Down
1 change: 1 addition & 0 deletions tests/selftest_receive_usage.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
. ${srcdir:=$(pwd)}/test-framework.sh
./receive &>librelp.out.log
cat librelp.out.log
check_output "Port is missing"
terminate
9 changes: 9 additions & 0 deletions tests/send-noconnect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# check that failed connect to receiver is gracefully handled and error
# message emitted.
# written 2018-11-20 by Rainer Gerhards, released under ASL 2.0
. ${srcdir:=$(pwd)}/test-framework.sh
./send -t 127.0.0.1 -p $TESTPORT -m "testmessage" $OPT_VERBOSE &> librelp.out.log

check_output "error opening connection"
terminate
27 changes: 25 additions & 2 deletions tests/send.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ static int num_messages = 0;
static size_t lenMsg = 0;
static relpClt_t *pRelpClt = NULL;

#define USR_MAGIC 0x1234FFee
struct usrdata { /* used for testing user pointer pass-back */
int magic;
char *progname;
};
struct usrdata *userdata = NULL;


static void __attribute__((format(printf, 1, 2)))
dbgprintf(char *fmt, ...)
Expand All @@ -58,9 +65,17 @@ void print_usage(void)
}

static void
onErr( __attribute__((unused)) void *pUsr, char *objinfo, char* errmesg, __attribute__((unused)) relpRetVal errcode)
onErr(void *pUsr, char *objinfo, char* errmesg, __attribute__((unused)) relpRetVal errcode)
{
printf("send: error '%s', object '%s'\n", errmesg, objinfo);
struct usrdata *pThis = (struct usrdata*) pUsr;
if(pUsr != userdata) {
fprintf(stderr, "send: pUsr NOT pointing to usrdata!\n");
}
if(pThis->magic != USR_MAGIC) {
fprintf(stderr, "send: pUsr magic incorrect in onErr, magic %8.8x "
"pUsr %p\n", pThis->magic, (void*) pThis);
}
printf("%s: error '%s', object '%s'\n", pThis->progname, errmesg, objinfo);
if(errFile != NULL) {
fprintf(errFile, "send: error '%s', object '%s'\n", errmesg, objinfo);
}
Expand Down Expand Up @@ -89,6 +104,10 @@ onAuthErr( __attribute__((unused)) void *pUsr, char *authinfo,
static void
exit_hdlr(void)
{
if(userdata != NULL) {
free(userdata->progname);
free(userdata);
}
if(errFile != NULL) {
fclose(errFile);
}
Expand Down Expand Up @@ -281,6 +300,10 @@ int main(int argc, char *argv[]) {
TRY(relpEngineSetEnableCmd(pRelpEngine, (unsigned char*)"syslog", eRelpCmdState_Required));
TRY(relpEngineCltConstruct(pRelpEngine, &pRelpClt));
TRY(relpCltSetTimeout(pRelpClt, timeout));
userdata = calloc(1, sizeof(struct usrdata));
userdata->magic = USR_MAGIC;
userdata->progname = strdup("send");
TRY(relpCltSetUsrPtr(pRelpClt, userdata));

if(bEnableTLS) {
TRY(relpCltEnableTLS(pRelpClt));
Expand Down
2 changes: 1 addition & 1 deletion tests/test-framework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# "config settings" for the testbench
TB_TIMEOUT_STARTUP=400 # 40 seconds - Solaris sometimes needs this...
TESTPORT=31514
export TESTPORT=31514
export OUTFILE=librelp.out.log
export valgrind="valgrind --malloc-fill=ff --free-fill=fe --log-fd=1"
#export OPT_VERBOSE=-v # uncomment for debugging
Expand Down

0 comments on commit 3e91258

Please sign in to comment.