diff --git a/nsock/tests/Makefile b/nsock/tests/Makefile index 360c8ef1f..fb2e2cc3e 100644 --- a/nsock/tests/Makefile +++ b/nsock/tests/Makefile @@ -17,7 +17,8 @@ SRC = tests_main.c \ logs.c \ connect.c \ ghlists.c \ - ghheaps.c + ghheaps.c \ + cancel.c OBJ = $(SRC:.c=.o) diff --git a/nsock/tests/basic.c b/nsock/tests/basic.c index cb11d58f3..f2e3897ef 100644 --- a/nsock/tests/basic.c +++ b/nsock/tests/basic.c @@ -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 -}; - diff --git a/nsock/tests/cancel.c b/nsock/tests/cancel.c new file mode 100644 index 000000000..42a1142ed --- /dev/null +++ b/nsock/tests/cancel.c @@ -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 +}; diff --git a/nsock/tests/tests_main.c b/nsock/tests/tests_main.c index ac7cda373..a88f612ee 100644 --- a/nsock/tests/tests_main.c +++ b/nsock/tests/tests_main.c @@ -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 };