Added extra cancelation tests.

Schedule and immediately cancel TCP, UDP and SSL connects.
These tests are now grouped in a "cancel" module.
This commit is contained in:
henri 2013-10-23 19:04:10 +00:00
parent 9c083de2cf
commit 457e9dcf62
4 changed files with 143 additions and 65 deletions

View file

@ -17,7 +17,8 @@ SRC = tests_main.c \
logs.c \
connect.c \
ghlists.c \
ghheaps.c
ghheaps.c \
cancel.c
OBJ = $(SRC:.c=.o)

View file

@ -43,60 +43,6 @@ static int basic_udata(void *tdata) {
}
static void cancel_handler(nsock_pool nsp, nsock_event nse, void *udata) {
int *ev_done = (int *)udata;
if (nse_status(nse) == NSE_STATUS_CANCELLED)
*ev_done = 1;
}
static int cancel_setup(void **tdata) {
struct basic_test_data *btd;
btd = calloc(1, sizeof(struct basic_test_data));
if (btd == NULL)
return -ENOMEM;
btd->nsp = nsp_new(NULL);
*tdata = btd;
return 0;
}
static int cancel_teardown(void *tdata) {
struct basic_test_data *btd = (struct basic_test_data *)tdata;
if (tdata) {
nsp_delete(btd->nsp);
free(tdata);
}
return 0;
}
static int cancel_run(void *tdata) {
struct basic_test_data *btd = (struct basic_test_data *)tdata;
struct sockaddr_in peer;
nsock_iod iod;
nsock_event_id id;
int done = 0;
iod = nsi_new(btd->nsp, NULL);
AssertNonNull(iod);
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
inet_aton("127.0.0.1", &peer.sin_addr);
id = nsock_connect_ssl(btd->nsp, iod, cancel_handler, 4000, (void *)&done,
(struct sockaddr *)&peer, sizeof(peer), IPPROTO_TCP,
PORT_TCPSSL, NULL);
nsock_event_cancel(btd->nsp, id, 1);
nsi_delete(iod, NSOCK_PENDING_SILENT);
return (done == 1) ? 0 : -ENOEXEC;
}
const struct test_case TestPoolUserData = {
.t_name = "nsock pool user data",
@ -104,11 +50,3 @@ const struct test_case TestPoolUserData = {
.t_run = basic_udata,
.t_teardown = basic_teardown
};
const struct test_case TestCancelSSLOperation = {
.t_name = "schedule and cancel SSL connect",
.t_setup = cancel_setup,
.t_run = cancel_run,
.t_teardown = cancel_teardown
};

134
nsock/tests/cancel.c Normal file
View file

@ -0,0 +1,134 @@
/*
* Nsock regression test suite
* Same license as nmap -- see http://nmap.org/book/man-legal.html
*/
#include "test-common.h"
struct basic_test_data {
nsock_pool nsp;
};
static void cancel_handler(nsock_pool nsp, nsock_event nse, void *udata) {
int *ev_done = (int *)udata;
if (nse_status(nse) == NSE_STATUS_CANCELLED)
*ev_done = 1;
}
static int cancel_setup(void **tdata) {
struct basic_test_data *btd;
btd = calloc(1, sizeof(struct basic_test_data));
if (btd == NULL)
return -ENOMEM;
btd->nsp = nsp_new(NULL);
*tdata = btd;
return 0;
}
static int cancel_teardown(void *tdata) {
struct basic_test_data *btd = (struct basic_test_data *)tdata;
if (tdata) {
nsp_delete(btd->nsp);
free(tdata);
}
return 0;
}
static int cancel_tcp_run(void *tdata) {
struct basic_test_data *btd = (struct basic_test_data *)tdata;
struct sockaddr_in peer;
nsock_iod iod;
nsock_event_id id;
int done = 0;
iod = nsi_new(btd->nsp, NULL);
AssertNonNull(iod);
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
inet_aton("127.0.0.1", &peer.sin_addr);
id = nsock_connect_tcp(btd->nsp, iod, cancel_handler, 4000, (void *)&done,
(struct sockaddr *)&peer, sizeof(peer), PORT_TCP);
nsock_event_cancel(btd->nsp, id, 1);
nsi_delete(iod, NSOCK_PENDING_SILENT);
return (done == 1) ? 0 : -ENOEXEC;
}
static int cancel_udp_run(void *tdata) {
struct basic_test_data *btd = (struct basic_test_data *)tdata;
struct sockaddr_in peer;
nsock_iod iod;
nsock_event_id id;
int done = 0;
iod = nsi_new(btd->nsp, NULL);
AssertNonNull(iod);
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
inet_aton("127.0.0.1", &peer.sin_addr);
id = nsock_connect_udp(btd->nsp, iod, cancel_handler, (void *)&done,
(struct sockaddr *)&peer, sizeof(peer), PORT_UDP);
nsock_event_cancel(btd->nsp, id, 1);
nsi_delete(iod, NSOCK_PENDING_SILENT);
return (done == 1) ? 0 : -ENOEXEC;
}
static int cancel_ssl_run(void *tdata) {
struct basic_test_data *btd = (struct basic_test_data *)tdata;
struct sockaddr_in peer;
nsock_iod iod;
nsock_event_id id;
int done = 0;
iod = nsi_new(btd->nsp, NULL);
AssertNonNull(iod);
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
inet_aton("127.0.0.1", &peer.sin_addr);
id = nsock_connect_ssl(btd->nsp, iod, cancel_handler, 4000, (void *)&done,
(struct sockaddr *)&peer, sizeof(peer), IPPROTO_TCP,
PORT_TCPSSL, NULL);
nsock_event_cancel(btd->nsp, id, 1);
nsi_delete(iod, NSOCK_PENDING_SILENT);
return (done == 1) ? 0 : -ENOEXEC;
}
const struct test_case TestCancelTCP = {
.t_name = "schedule and cancel TCP connect",
.t_setup = cancel_setup,
.t_run = cancel_tcp_run,
.t_teardown = cancel_teardown
};
const struct test_case TestCancelUDP = {
.t_name = "schedule and cancel UDP pseudo-connect",
.t_setup = cancel_setup,
.t_run = cancel_udp_run,
.t_teardown = cancel_teardown
};
const struct test_case TestCancelSSL = {
.t_name = "schedule and cancel SSL connect",
.t_setup = cancel_setup,
.t_run = cancel_ssl_run,
.t_teardown = cancel_teardown
};

View file

@ -26,7 +26,6 @@
char *socket_strerror(int errnum);
extern const struct test_case TestPoolUserData;
extern const struct test_case TestCancelOperation;
extern const struct test_case TestTimer;
extern const struct test_case TestLogLevels;
extern const struct test_case TestErrLevels;
@ -34,12 +33,14 @@ extern const struct test_case TestConnectTCP;
extern const struct test_case TestGHLists;
extern const struct test_case TestGHHeaps;
extern const struct test_case TestHeapOrdering;
extern const struct test_case TestCancelTCP;
extern const struct test_case TestCancelUDP;
extern const struct test_case TestCancelSSL;
static const struct test_case *TestCases[] = {
/* ---- basic.c */
&TestPoolUserData,
&TestCancelOperation,
/* ---- timer.c */
&TestTimer,
/* ---- logs.c */
@ -52,6 +53,10 @@ static const struct test_case *TestCases[] = {
/* ---- ghheaps.c */
&TestGHHeaps,
&TestHeapOrdering,
/* ---- cancel.c */
&TestCancelTCP,
&TestCancelUDP,
&TestCancelSSL,
NULL
};