1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-04 13:49:03 +00:00

Added simple TCP connect test and setup ncat listeners.

The listeners are ncat-based echo servers:
  - UDP
  - TCP
  - TCP + SSL
This commit is contained in:
henri
2013-05-10 08:26:48 +00:00
parent 76db6d8de3
commit 3776852557
5 changed files with 127 additions and 2 deletions

View File

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

77
nsock/tests/connect.c Normal file
View File

@@ -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 <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
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
};

View File

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

View File

@@ -16,6 +16,11 @@
#include <nsock.h>
#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", \

View File

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