summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorAlexander Bluhm <bluhm@cvs.openbsd.org>2020-01-17 20:45:51 +0000
committerAlexander Bluhm <bluhm@cvs.openbsd.org>2020-01-17 20:45:51 +0000
commit379cc1d7bfaba1dd861b5cdaf35d6b4a71ba47d1 (patch)
treeb25e8f0ac98c8d55ba757e989aed60b738aa32a2 /regress
parent033dac07bdc355d1e9ab5047eae3cf41f38348f7 (diff)
Test the netcat -N feature. The test program can send and receive
messages, shutdown write, and wait for EOF in arbitray order. Since revision 1.211 of netcat.c some of the cases fail.
Diffstat (limited to 'regress')
-rw-r--r--regress/usr.bin/nc/Makefile132
-rw-r--r--regress/usr.bin/nc/client-tcp.c24
-rw-r--r--regress/usr.bin/nc/server-tcp.c27
-rw-r--r--regress/usr.bin/nc/util.c81
-rw-r--r--regress/usr.bin/nc/util.h11
5 files changed, 242 insertions, 33 deletions
diff --git a/regress/usr.bin/nc/Makefile b/regress/usr.bin/nc/Makefile
index 30ae4b825cf..bf4ceea9ede 100644
--- a/regress/usr.bin/nc/Makefile
+++ b/regress/usr.bin/nc/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.4 2020/01/16 21:11:17 bluhm Exp $
+# $OpenBSD: Makefile,v 1.5 2020/01/17 20:45:50 bluhm Exp $
# Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org>
#
@@ -893,13 +893,23 @@ run-unix-dgram-keep:
# XXX message succeeded is missing
! grep 'Connection to 127.0.0.1 .* succeeded!' client.err
-### TCP with custom peer
+### TCP with test peer
-REGRESS_TARGETS += run-tcp-custom
-run-tcp-custom: server-tcp client-tcp
+REGRESS_TARGETS += run-tcp-test
+run-tcp-test: server-tcp client-tcp
@echo '======== $@ ========'
+ # test the test tools
./server-tcp -s greeting -r command 127.0.0.1 0 >server.port
- ./client-tcp -r greeting -s command 127.0.0.1 ${PORT}
+ ./client-tcp -r greeting -s command 127.0.0.1 ${PORT} >client.port
+
+REGRESS_TARGETS += run-tcp-test-shutdown
+run-tcp-test-shutdown: server-tcp client-tcp
+ @echo '======== $@ ========'
+ # test the test tools
+ ./server-tcp -s greeting -N -r command -E 127.0.0.1 0 >server.port
+ ./client-tcp -r greeting -E -s command -N 127.0.0.1 ${PORT} >client.port
+
+# TCP netcat server with test client
REGRESS_TARGETS += run-tcp-server
run-tcp-server: client-tcp
@@ -907,15 +917,73 @@ run-tcp-server: client-tcp
${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
${LISTEN_WAIT}
${PORT_GET}
- ./client-tcp -r greeting -s command 127.0.0.1 ${PORT}
+ # test client read from netcat, then send line and exit
+ ./client-tcp -r greeting -s command 127.0.0.1 ${PORT} >client.port
${TRANSFER_SERVER_WAIT}
grep '^command$$' server.out
grep 'Listening on 127.0.0.1 ' server.err
grep 'Connection received on 127.0.0.1 ' server.err
+REGRESS_TARGETS += run-tcp-server-eof
+run-tcp-server-eof: client-tcp
+ @echo '======== $@ ========'
+ ${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
+ ${LISTEN_WAIT}
+ ${PORT_GET}
+ # test client read from netcat, then send line, shutdown, wait for eof
+ ./client-tcp -r greeting -s command -N -E 127.0.0.1 ${PORT} >client.port
+ ${TRANSFER_SERVER_WAIT}
+ grep '^command$$' server.out
+ grep 'Listening on 127.0.0.1 ' server.err
+ grep 'Connection received on 127.0.0.1 ' server.err
+
+REGRESS_TARGETS += run-tcp-server-reverse-eof
+run-tcp-server-reverse-eof: client-tcp
+ @echo '======== $@ ========'
+ ${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
+ ${LISTEN_WAIT}
+ ${PORT_GET}
+ # test client send to netcat, shutdown, then read line, wait for eof
+ ./client-tcp -s command -N -r greeting -E 127.0.0.1 ${PORT} >client.port
+ ${TRANSFER_SERVER_WAIT}
+ grep '^command$$' server.out
+ grep 'Listening on 127.0.0.1 ' server.err
+ grep 'Connection received on 127.0.0.1 ' server.err
+
+REGRESS_TARGETS += run-tcp-server-shutdown-eof
+run-tcp-server-shutdown-eof: client-tcp
+ @echo '======== $@ ========'
+ # netcat calls shutdown on output after EOF on input
+ ${SERVER_NC} -N -n -v -l 127.0.0.1 0 ${SERVER_BG}
+ ${LISTEN_WAIT}
+ ${PORT_GET}
+ # test client read from netcat, then send line, wait for eof, shutdown
+ ./client-tcp -r greeting -s command -E -N 127.0.0.1 ${PORT} >client.port
+ ${TRANSFER_SERVER_WAIT}
+ grep '^command$$' server.out
+ grep 'Listening on 127.0.0.1 ' server.err
+ grep 'Connection received on 127.0.0.1 ' server.err
+
+REGRESS_TARGETS += run-tcp-server-shutdown-reverse-eof
+run-tcp-server-shutdown-reverse-eof: client-tcp
+ @echo '======== $@ ========'
+ # netcat calls shutdown on output after EOF on input
+ ${SERVER_NC} -N -n -v -l 127.0.0.1 0 ${SERVER_BG}
+ ${LISTEN_WAIT}
+ ${PORT_GET}
+ # test client send to netcat, shutdown, then read line, wait for eof
+ ./client-tcp -s command -N -r greeting -E 127.0.0.1 ${PORT} >client.port
+ ${TRANSFER_SERVER_WAIT}
+ grep '^command$$' server.out
+ grep 'Listening on 127.0.0.1 ' server.err
+ grep 'Connection received on 127.0.0.1 ' server.err
+
+# TCP netcat client with test server
+
REGRESS_TARGETS += run-tcp-client
run-tcp-client: server-tcp
@echo '======== $@ ========'
+ # test server send to netcat, then read line and exit
./server-tcp -s greeting -r command 127.0.0.1 0 >server.port
${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
${CONNECT_WAIT}
@@ -923,6 +991,58 @@ run-tcp-client: server-tcp
grep '^greeting$$' client.out
grep 'Connection to 127.0.0.1 .* succeeded!' client.err
+REGRESS_TARGETS += run-tcp-client-eof
+run-tcp-client-eof: server-tcp
+ @echo '======== $@ ========'
+ # test server send to netcat, shutdown, then read line, wait for eof
+ ./server-tcp -s greeting -N -r command -E 127.0.0.1 0 >server.port
+ ${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
+ ${CONNECT_WAIT}
+ ${TRANSFER_CLIENT_WAIT}
+ grep '^greeting$$' client.out
+ grep 'Connection to 127.0.0.1 .* succeeded!' client.err
+
+REGRESS_TARGETS += run-tcp-client-reverse-eof
+run-tcp-client-reverse-eof: server-tcp
+ @echo '======== $@ ========'
+ # test server read from netcat, then read line, wait for eof, shutdown
+ ./server-tcp -r command -s greeting -E -N 127.0.0.1 0 >server.port
+ ${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
+ ${CONNECT_WAIT}
+ ${TRANSFER_CLIENT_WAIT}
+ grep '^greeting$$' client.out
+ grep 'Connection to 127.0.0.1 .* succeeded!' client.err
+
+REGRESS_TARGETS += run-tcp-client-shutdown-eof
+run-tcp-client-shutdown-eof: server-tcp
+ @echo '======== $@ ========'
+ # test server send to netcat, shutdown, then read line, wait for eof
+ ./server-tcp -s greeting -N -r command -E 127.0.0.1 0 >server.port
+ # netcat calls shutdown on output after EOF on input
+ ${CLIENT_NC} -N -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
+ ${CONNECT_WAIT}
+ ${TRANSFER_CLIENT_WAIT}
+ grep '^greeting$$' client.out
+ grep 'Connection to 127.0.0.1 .* succeeded!' client.err
+
+REGRESS_TARGETS += run-tcp-client-shutdown-reverse-eof
+run-tcp-client-shutdown-reverse-eof: server-tcp
+ @echo '======== $@ ========'
+ # test server read from netcat, wait for eof, then read line, shutdown
+ ./server-tcp -r command -E -s greeting -N 127.0.0.1 0 >server.port
+ # netcat calls shutdown on output after EOF on input
+ ${CLIENT_NC} -N -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
+ ${CONNECT_WAIT}
+ ${TRANSFER_CLIENT_WAIT}
+ grep '^greeting$$' client.out
+ grep 'Connection to 127.0.0.1 .* succeeded!' client.err
+
+REGRESS_EXPECTED_FAILURES = \
+ run-tcp-server-shutdown-eof \
+ run-tcp-server-shutdown-reverse-eof \
+ run-tcp-client-shutdown-eof \
+ run-tcp-client-shutdown-reverse-eof
+
.PHONY: ${REGRESS_SETUP} ${REGRESS_CLEANUP} ${REGRESS_TARGETS}
### create certificates for TLS
diff --git a/regress/usr.bin/nc/client-tcp.c b/regress/usr.bin/nc/client-tcp.c
index 09938848409..b26c3f3695f 100644
--- a/regress/usr.bin/nc/client-tcp.c
+++ b/regress/usr.bin/nc/client-tcp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: client-tcp.c,v 1.1 2020/01/16 21:11:17 bluhm Exp $ */
+/* $OpenBSD: client-tcp.c,v 1.2 2020/01/17 20:45:50 bluhm Exp $ */
/*
* Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org>
@@ -36,6 +36,8 @@ void __dead
usage(void)
{
fprintf(stderr, "client [-r rcvmsg] [-s sndmsg] host port\n"
+ " -E wait for EOF\n"
+ " -N shutdown write\n"
" -r rcvmsg receive from server and check message\n"
" -s sndmsg send message to server\n");
exit(2);
@@ -45,16 +47,20 @@ int
main(int argc, char *argv[])
{
const char *host, *port;
- const char *rcvmsg = NULL, *sndmsg = NULL;
+ struct task todo[100];
+ size_t tlen = 0;
int ch, s;
- while ((ch = getopt(argc, argv, "r:s:")) != -1) {
+ while ((ch = getopt(argc, argv, "ENr:s:")) != -1) {
switch (ch) {
+ case 'E':
+ case 'N':
case 'r':
- rcvmsg = optarg;
- break;
case 's':
- sndmsg = optarg;
+ if (tlen >= sizeof(todo) / sizeof(todo[0]))
+ errx(1, "too many tasks");
+ task_enqueue(&todo[tlen], ch, optarg);
+ tlen++;
break;
default:
usage();
@@ -74,11 +80,7 @@ main(int argc, char *argv[])
s = connect_socket(host, port);
print_sockname(s);
print_peername(s);
- if (rcvmsg != NULL)
- receive_line(s, rcvmsg);
- if (sndmsg != NULL)
- send_line(s, sndmsg);
-
+ task_run(s, todo, tlen);
if (close(s) == -1)
err(1, "close");
diff --git a/regress/usr.bin/nc/server-tcp.c b/regress/usr.bin/nc/server-tcp.c
index 07a2a49500d..31c619f5fad 100644
--- a/regress/usr.bin/nc/server-tcp.c
+++ b/regress/usr.bin/nc/server-tcp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server-tcp.c,v 1.1 2020/01/16 21:11:17 bluhm Exp $ */
+/* $OpenBSD: server-tcp.c,v 1.2 2020/01/17 20:45:50 bluhm Exp $ */
/*
* Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org>
@@ -37,6 +37,8 @@ void __dead
usage(void)
{
fprintf(stderr, "server-tcp [-r rcvmsg] [-s sndmsg] host port\n"
+ " -E wait for EOF\n"
+ " -N shutdown write\n"
" -r rcvmsg receive from client and check message\n"
" -s sndmsg send message to client\n");
exit(2);
@@ -46,16 +48,20 @@ int
main(int argc, char *argv[])
{
const char *host, *port;
- const char *rcvmsg = NULL, *sndmsg = NULL;
+ struct task todo[100];
+ size_t tlen = 0;
int ch, s;
- while ((ch = getopt(argc, argv, "r:s:")) != -1) {
+ while ((ch = getopt(argc, argv, "ENr:s:")) != -1) {
switch (ch) {
+ case 'E':
+ case 'N':
case 'r':
- rcvmsg = optarg;
- break;
case 's':
- sndmsg = optarg;
+ if (tlen >= sizeof(todo) / sizeof(todo[0]))
+ errx(1, "too many tasks");
+ task_enqueue(&todo[tlen], ch, optarg);
+ tlen++;
break;
default:
usage();
@@ -79,7 +85,8 @@ main(int argc, char *argv[])
case -1:
err(1, "fork");
case 0:
- /* child continues */
+ /* child continues, set timer for new process */
+ alarm_timeout();
break;
default:
/* parent exits and test runs in parallel */
@@ -87,11 +94,7 @@ main(int argc, char *argv[])
}
s = accept_socket(s);
- if (sndmsg != NULL)
- send_line(s, sndmsg);
- if (rcvmsg != NULL)
- receive_line(s, rcvmsg);
-
+ task_run(s, todo, tlen);
if (close(s) == -1)
err(1, "close");
diff --git a/regress/usr.bin/nc/util.c b/regress/usr.bin/nc/util.c
index ac96190fc8d..46c8bfe1520 100644
--- a/regress/usr.bin/nc/util.c
+++ b/regress/usr.bin/nc/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.1 2020/01/16 21:11:17 bluhm Exp $ */
+/* $OpenBSD: util.c,v 1.2 2020/01/17 20:45:50 bluhm Exp $ */
/*
* Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org>
@@ -30,6 +30,52 @@
#include "util.h"
void
+task_enqueue(struct task *todo, int ch, const char *msg)
+{
+ switch (ch) {
+ case 'E':
+ todo->t_type = TEOF;
+ todo->t_msg = NULL;
+ break;
+ case 'N':
+ todo->t_type = TDWN;
+ todo->t_msg = NULL;
+ break;
+ case 'r':
+ todo->t_type = TRCV;
+ todo->t_msg = msg;
+ break;
+ case 's':
+ todo->t_type = TSND;
+ todo->t_msg = msg;
+ break;
+ }
+}
+
+void
+task_run(int s, struct task *todolist, size_t tlen)
+{
+ size_t t;
+
+ for (t = 0; t < tlen; t++) {
+ switch(todolist[t].t_type) {
+ case TEOF:
+ receive_eof(s);
+ break;
+ case TDWN:
+ send_shutdown(s);
+ break;
+ case TRCV:
+ receive_line(s, todolist[t].t_msg);
+ break;
+ case TSND:
+ send_line(s, todolist[t].t_msg);
+ break;
+ }
+ }
+}
+
+void
alarm_timeout(void)
{
/* just abort after 10 seconds */
@@ -73,6 +119,35 @@ print_peername(int s)
}
void
+receive_eof(int s)
+{
+ char buf[100];
+ size_t len;
+ ssize_t n;
+
+ n = recv(s, buf, sizeof(buf) - 1, 0);
+ if (n == -1)
+ err(1, "recv");
+ if (n == 0) {
+ fprintf(stderr, "<<< EOF\n");
+ return;
+ }
+ len = n;
+ buf[len] = '\0';
+ if (buf[len - 1] == '\n')
+ buf[--len] = '\0';
+ fprintf(stderr, "<<< %s\n", buf);
+ errx(1, "expected receive EOF, got '%s'", buf);
+}
+
+void
+send_shutdown(int s)
+{
+ if (shutdown(s, SHUT_WR) == -1)
+ err(1, "shutdown");
+}
+
+void
receive_line(int s, const char *msg)
{
char buf[100];
@@ -80,9 +155,9 @@ receive_line(int s, const char *msg)
ssize_t n;
len = 0;
- while (len < sizeof(buf)) {
+ while (len < sizeof(buf) - 1) {
off = len;
- n = recv(s, buf + off, sizeof(buf) - off, 0);
+ n = recv(s, buf + off, sizeof(buf) - 1 - off, 0);
if (n == -1)
err(1, "recv");
if (n == 0) {
diff --git a/regress/usr.bin/nc/util.h b/regress/usr.bin/nc/util.h
index f70b6865ac7..71765a486ea 100644
--- a/regress/usr.bin/nc/util.h
+++ b/regress/usr.bin/nc/util.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.h,v 1.1 2020/01/16 21:11:17 bluhm Exp $ */
+/* $OpenBSD: util.h,v 1.2 2020/01/17 20:45:50 bluhm Exp $ */
/*
* Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org>
@@ -16,8 +16,17 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+struct task {
+ const char *t_msg;
+ enum { TEOF, TDWN, TRCV, TSND } t_type;
+};
+
+void task_enqueue(struct task *, int, const char *);
+void task_run(int, struct task *, size_t);
void alarm_timeout(void);
void print_sockname(int);
void print_peername(int);
+void receive_eof(int);
+void send_shutdown(int);
void receive_line(int, const char *);
void send_line(int, const char *);