From 036c918e5d778a941ef62c84a52015d407b06bba Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Tue, 20 Nov 2018 11:25:07 +0100 Subject: [PATCH] testbench: check functionality for pUsr handling --- tests/Makefile.am | 3 +++ tests/dummyclient.py | 10 ++++++++++ tests/receive-emptyconnect.sh | 12 ++++++++++++ tests/receive.c | 26 ++++++++++++++++++++++++-- tests/selftest_receive_usage.sh | 1 + tests/send-noconnect.sh | 9 +++++++++ tests/send.c | 27 +++++++++++++++++++++++++-- tests/test-framework.sh | 2 +- 8 files changed, 85 insertions(+), 5 deletions(-) create mode 100755 tests/dummyclient.py create mode 100755 tests/receive-emptyconnect.sh create mode 100755 tests/send-noconnect.sh diff --git a/tests/Makefile.am b/tests/Makefile.am index 5663dec6..5e14ac91 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 @@ -43,6 +45,7 @@ endif EXTRA_DIST=$(TESTS) \ $(VALGRIND_TESTS) \ + dummyclient.py \ test-framework.sh \ receive.c \ send.c \ diff --git a/tests/dummyclient.py b/tests/dummyclient.py new file mode 100755 index 00000000..6f99c798 --- /dev/null +++ b/tests/dummyclient.py @@ -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() diff --git a/tests/receive-emptyconnect.sh b/tests/receive-emptyconnect.sh new file mode 100755 index 00000000..994c5f55 --- /dev/null +++ b/tests/receive-emptyconnect.sh @@ -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 diff --git a/tests/receive.c b/tests/receive.c index e872da9e..9da2a7fc 100644 --- a/tests/receive.c +++ b/tests/receive.c @@ -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)()) { @@ -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); } @@ -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); } @@ -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)); } diff --git a/tests/selftest_receive_usage.sh b/tests/selftest_receive_usage.sh index c4ac8bb1..923d8b50 100755 --- a/tests/selftest_receive_usage.sh +++ b/tests/selftest_receive_usage.sh @@ -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 diff --git a/tests/send-noconnect.sh b/tests/send-noconnect.sh new file mode 100755 index 00000000..1e50750e --- /dev/null +++ b/tests/send-noconnect.sh @@ -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 diff --git a/tests/send.c b/tests/send.c index b3a0954a..d8ccfd55 100644 --- a/tests/send.c +++ b/tests/send.c @@ -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, ...) @@ -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); } @@ -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); } @@ -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)); diff --git a/tests/test-framework.sh b/tests/test-framework.sh index 6947faf4..7960ab85 100644 --- a/tests/test-framework.sh +++ b/tests/test-framework.sh @@ -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