From 37768525577cea2896e42fa5513b6cdcdae288bf Mon Sep 17 00:00:00 2001 From: henri Date: Fri, 10 May 2013 08:26:48 +0000 Subject: [PATCH] Added simple TCP connect test and setup ncat listeners. The listeners are ncat-based echo servers: - UDP - TCP - TCP + SSL --- nsock/tests/Makefile | 3 +- nsock/tests/connect.c | 77 +++++++++++++++++++++++++++++++++++++++ nsock/tests/run_tests.sh | 41 ++++++++++++++++++++- nsock/tests/test-common.h | 5 +++ nsock/tests/tests_main.c | 3 ++ 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 nsock/tests/connect.c diff --git a/nsock/tests/Makefile b/nsock/tests/Makefile index 5a1d25e9e..c86c330e7 100644 --- a/nsock/tests/Makefile +++ b/nsock/tests/Makefile @@ -14,7 +14,8 @@ LDFLAGS = -lssl -lpcap -lcrypto SRC = tests_main.c \ basic.c \ timer.c \ - logs.c + logs.c \ + connect.c OBJ = $(SRC:.c=.o) diff --git a/nsock/tests/connect.c b/nsock/tests/connect.c new file mode 100644 index 000000000..188ee2a04 --- /dev/null +++ b/nsock/tests/connect.c @@ -0,0 +1,77 @@ +/* + * Nsock regression test suite + * Same license as nmap -- see http://nmap.org/book/man-legal.html + */ + +#include "test-common.h" +#include +#include +#include +#include + + +struct connect_test_data { + nsock_pool nsp; + nsock_iod nsi; + enum nse_status connect_result; +}; + + +static void connect_handler(nsock_pool nsp, nsock_event nse, void *udata) { + struct connect_test_data *ctd; + + ctd = (struct connect_test_data *)nsp_getud(nsp); + ctd->connect_result = nse_status(nse); +} + +static int connect_setup(void **tdata) { + struct connect_test_data *ctd; + + ctd = calloc(1, sizeof(struct connect_test_data)); + if (ctd == NULL) + return -ENOMEM; + + ctd->nsp = nsp_new(ctd); + AssertNonNull(ctd->nsp); + + ctd->nsi = nsi_new(ctd->nsp, NULL); + AssertNonNull(ctd->nsi); + + *tdata = ctd; + return 0; +} + +static int connect_teardown(void *tdata) { + struct connect_test_data *ctd = (struct connect_test_data *)tdata; + + if (tdata) { + nsi_delete(ctd->nsi, NSOCK_PENDING_SILENT); /* nsp_delete would also handle it */ + nsp_delete(ctd->nsp); + free(tdata); + } + return 0; +} + +static int connect_tcp(void *tdata) { + struct connect_test_data *ctd = (struct connect_test_data *)tdata; + struct sockaddr_in peer; + + memset(&peer, 0, sizeof(peer)); + peer.sin_family = AF_INET; + inet_aton("127.0.0.1", &peer.sin_addr); + + nsock_connect_tcp(ctd->nsp, ctd->nsi, connect_handler, 4000, NULL, + (struct sockaddr *)&peer, sizeof(peer), PORT_TCP); + + nsock_loop(ctd->nsp, 4000); + AssertEqual(ctd->connect_result, NSE_STATUS_SUCCESS); + return 0; +} + + +const struct test_case TestConnectTCP = { + .t_name = "simple tcp connection", + .t_setup = connect_setup, + .t_run = connect_tcp, + .t_teardown = connect_teardown +}; diff --git a/nsock/tests/run_tests.sh b/nsock/tests/run_tests.sh index 7e5688f34..8ddf03819 100755 --- a/nsock/tests/run_tests.sh +++ b/nsock/tests/run_tests.sh @@ -3,8 +3,14 @@ # nsock regression test suite # Same license as nmap -- see http://nmap.org/book/man-legal.html +# hackish, I should consider using a configuration file. +PORT_UDP=$(grep "PORT_UDP " test-common.h | awk '{print $3}') +PORT_TCP=$(grep "PORT_TCP " test-common.h | awk '{print $3}') +PORT_TCPSSL=$(grep "PORT_TCPSSL " test-common.h | awk '{print $3}') + EXEC_MAIN=./tests_main + if [ -n "$1" ] then case "$1" in @@ -32,4 +38,37 @@ then esac fi -$TRACER $EXEC_MAIN + +function setup_echo_udp() { + ncat -l --udp --sh-exec cat localhost $PORT_UDP & + pid_udp=$! + echo "started UDP listener on port $PORT_UDP (pid $pid_udp)" +} + +function setup_echo_tcp() { + ncat -l --keep-open --sh-exec cat localhost $PORT_TCP & + pid_tcp=$! + echo "started TCP listener on port $PORT_TCP (pid $pid_tcp)" +} + +function setup_echo_tcpssl() { + ncat -l --ssl --keep-open --sh-exec cat localhost $PORT_TCPSSL & + pid_tcpssl=$! + echo "started TCP SSL listener on port $PORT_TCPSSL (pid $pid_tcpssl)" +} + +function cleanup_all() { + kill -s KILL $@ 2>&1 >> /dev/null +} + +function main() { + setup_echo_udp $PORT_UDP + setup_echo_tcp $PORT_TCP + setup_echo_tcpssl $PORT_TCPSSL + + $TRACER $EXEC_MAIN + + cleanup_all $pid_udp $pid_tcp $pid_tcpssl +} + +main diff --git a/nsock/tests/test-common.h b/nsock/tests/test-common.h index 4ffd571ad..832474493 100644 --- a/nsock/tests/test-common.h +++ b/nsock/tests/test-common.h @@ -16,6 +16,11 @@ #include +#define PORT_UDP 55234 +#define PORT_TCP 55235 +#define PORT_TCPSSL 55236 + + #define __ASSERT_BASE(stmt) do { \ if (!(stmt)) { \ fprintf(stderr, "(%s:%d) Assertion failed: " #stmt "\n", \ diff --git a/nsock/tests/tests_main.c b/nsock/tests/tests_main.c index 32f499cf8..530dfc9ee 100644 --- a/nsock/tests/tests_main.c +++ b/nsock/tests/tests_main.c @@ -19,6 +19,7 @@ extern const struct test_case TestPoolUserData; extern const struct test_case TestTimer; extern const struct test_case TestLogLevels; extern const struct test_case TestErrLevels; +extern const struct test_case TestConnectTCP; static const struct test_case *TestCases[] = { @@ -29,6 +30,8 @@ static const struct test_case *TestCases[] = { /* ---- logs.c */ &TestLogLevels, &TestErrLevels, + /* ---- connect.c */ + &TestConnectTCP, NULL };