Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/net/tcp tests: add tests for invalid extra operations ...



details:   https://anonhg.NetBSD.org/src/rev/22621744b154
branches:  trunk
changeset: 372250:22621744b154
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Fri Nov 04 08:01:42 2022 +0000

description:
tests: add tests for invalid extra operations on a shutdown socket

The tests cover some error paths that normally happen.

diffstat:

 distrib/sets/lists/debug/mi     |    3 +-
 distrib/sets/lists/tests/mi     |    5 +-
 etc/mtree/NetBSD.dist.tests     |    3 +-
 tests/net/tcp/Makefile          |    8 +-
 tests/net/tcp/t_tcp_shutdown.sh |   85 ++++++++++++++++++++++++
 tests/net/tcp/tcp_shutdown.c    |  137 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 236 insertions(+), 5 deletions(-)

diffs (truncated from 314 to 300 lines):

diff -r 96800972ebfc -r 22621744b154 distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Fri Nov 04 07:54:27 2022 +0000
+++ b/distrib/sets/lists/debug/mi       Fri Nov 04 08:01:42 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.389 2022/08/28 07:30:41 christos Exp $
+# $NetBSD: mi,v 1.390 2022/11/04 08:01:42 ozaki-r Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib                                      comp-sys-usr            compatdir
 ./usr/lib/i18n/libBIG5_g.a                     comp-c-debuglib         debuglib,compatfile
@@ -2437,6 +2437,7 @@
 ./usr/libdata/debug/usr/tests/net/sys/t_listen.debug                   tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/net/sys/t_rfc6056.debug                  tests-net-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/net/sys/t_socketpair.debug               tests-obsolete          obsolete,compattestfile
+./usr/libdata/debug/usr/tests/net/tcp/tcp_shutdown.debug               tests-net-debug         debug,atf,rump
 ./usr/libdata/debug/usr/tests/rump/modautoload/t_modautoload.debug     tests-syscall-debug     debug,atf,rump
 ./usr/libdata/debug/usr/tests/rump/rumpkern/h_client/h_forkcli.debug                   tests-syscall-debug     debug,atf,rump
 ./usr/libdata/debug/usr/tests/rump/rumpkern/h_client/h_reboot.debug                    tests-obsolete          obsolete
diff -r 96800972ebfc -r 22621744b154 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Fri Nov 04 07:54:27 2022 +0000
+++ b/distrib/sets/lists/tests/mi       Fri Nov 04 08:01:42 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1226 2022/11/04 07:54:27 ozaki-r Exp $
+# $NetBSD: mi,v 1.1227 2022/11/04 08:01:42 ozaki-r Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -178,6 +178,7 @@
 ./usr/libdata/debug/usr/tests/net/mcast                        tests-net-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/net/net                  tests-net-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/net/sys                  tests-net-debug         compattestfile,atf
+./usr/libdata/debug/usr/tests/net/tcp                  tests-net-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/rump                     tests-syscall-debug     compattestfile,atf
 ./usr/libdata/debug/usr/tests/rump/modautoload         tests-syscall-debug     compattestfile,atf
 ./usr/libdata/debug/usr/tests/rump/rumpkern            tests-syscall-debug     compattestfile,atf
@@ -4286,7 +4287,9 @@
 ./usr/tests/net/tcp                                    tests-net-tests         compattestfile,atf
 ./usr/tests/net/tcp/Atffile                            tests-net-tests         atf,rump
 ./usr/tests/net/tcp/Kyuafile                           tests-net-tests         atf,rump,kyua
+./usr/tests/net/tcp/tcp_shutdown                       tests-net-tests         atf,rump
 ./usr/tests/net/tcp/t_tcp_nc                           tests-net-tests         atf,rump
+./usr/tests/net/tcp/t_tcp_shutdown                     tests-net-tests         atf,rump
 ./usr/tests/net/wireguard                              tests-obsolete          obsolete
 ./usr/tests/net/wireguard/Atffile                      tests-obsolete          obsolete
 ./usr/tests/net/wireguard/Kyuafile                     tests-obsolete          obsolete
diff -r 96800972ebfc -r 22621744b154 etc/mtree/NetBSD.dist.tests
--- a/etc/mtree/NetBSD.dist.tests       Fri Nov 04 07:54:27 2022 +0000
+++ b/etc/mtree/NetBSD.dist.tests       Fri Nov 04 08:01:42 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: NetBSD.dist.tests,v 1.194 2022/11/02 09:37:56 ozaki-r Exp $
+#      $NetBSD: NetBSD.dist.tests,v 1.195 2022/11/04 08:01:42 ozaki-r Exp $
 
 ./usr/libdata/debug/usr/tests
 ./usr/libdata/debug/usr/tests/atf
@@ -158,6 +158,7 @@
 ./usr/libdata/debug/usr/tests/net/mcast
 ./usr/libdata/debug/usr/tests/net/net
 ./usr/libdata/debug/usr/tests/net/sys
+./usr/libdata/debug/usr/tests/net/tcp
 ./usr/libdata/debug/usr/tests/rump
 ./usr/libdata/debug/usr/tests/rump/modautoload
 ./usr/libdata/debug/usr/tests/rump/rumpkern
diff -r 96800972ebfc -r 22621744b154 tests/net/tcp/Makefile
--- a/tests/net/tcp/Makefile    Fri Nov 04 07:54:27 2022 +0000
+++ b/tests/net/tcp/Makefile    Fri Nov 04 08:01:42 2022 +0000
@@ -1,13 +1,17 @@
-# $NetBSD: Makefile,v 1.1 2022/11/02 09:37:56 ozaki-r Exp $
+# $NetBSD: Makefile,v 1.2 2022/11/04 08:01:42 ozaki-r Exp $
 #
 
 .include <bsd.own.mk>
 
 TESTSDIR=      ${TESTSBASE}/net/tcp
 
-.for name in tcp_nc
+.for name in tcp_nc tcp_shutdown
 TESTS_SH+=             t_${name}
 TESTS_SH_SRC_t_${name}=        ../net_common.sh t_${name}.sh
 .endfor
 
+PROGS=                 tcp_shutdown
+MAN.tcp_shutdown=      # empty
+BINDIR.tcp_shutdown=   ${TESTSDIR}
+
 .include <bsd.test.mk>
diff -r 96800972ebfc -r 22621744b154 tests/net/tcp/t_tcp_shutdown.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/tcp/t_tcp_shutdown.sh   Fri Nov 04 08:01:42 2022 +0000
@@ -0,0 +1,85 @@
+#      $NetBSD: t_tcp_shutdown.sh,v 1.1 2022/11/04 08:01:42 ozaki-r Exp $
+#
+# Copyright (c) 2022 Internet Initiative Japan Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+SOCK=unix://tcp_shutdown
+BUS=./bus_tcp_shutdown
+
+DEBUG=${DEBUG:-false}
+
+setup_servers()
+{
+
+       rump_server_start $SOCK
+       rump_server_add_iface $SOCK shmif0 $BUS
+}
+
+test_tcp_shutdown()
+{
+       local target=$1
+       local prog="$(atf_get_srcdir)/tcp_shutdown"
+
+       export RUMP_SERVER=$SOCK
+       atf_check -s exit:0 $HIJACKING $prog $target
+}
+
+add_test_tcp_shutdown()
+{
+       local target=$1
+       local name= desc=
+
+       desc="Test TCP; call $target after shtudown"
+       name="tcp_shutdown_${target}"
+
+       atf_test_case ${name} cleanup
+       eval "
+           ${name}_head() {
+               atf_set descr \"$desc\"
+               atf_set require.progs rump_server
+           }
+           ${name}_body() {
+               setup_servers
+               test_tcp_shutdown $target
+               rump_server_destroy_ifaces
+           }
+           ${name}_cleanup() {
+               \$DEBUG && dump
+               cleanup
+           }
+       "
+       atf_add_test_case ${name}
+}
+
+atf_init_test_cases()
+{
+
+       add_test_tcp_shutdown bind
+       add_test_tcp_shutdown connect
+       add_test_tcp_shutdown getsockname
+       add_test_tcp_shutdown listen
+       add_test_tcp_shutdown setsockopt
+       add_test_tcp_shutdown shutdown
+}
diff -r 96800972ebfc -r 22621744b154 tests/net/tcp/tcp_shutdown.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/tcp/tcp_shutdown.c      Fri Nov 04 08:01:42 2022 +0000
@@ -0,0 +1,137 @@
+/*     $NetBSD: tcp_shutdown.c,v 1.1 2022/11/04 08:01:42 ozaki-r Exp $ */
+
+/*-
+ * Copyright (c) 2022 Internet Initiative Japan Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+#ifdef __RCSID
+__RCSID("$NetBSD: tcp_shutdown.c,v 1.1 2022/11/04 08:01:42 ozaki-r Exp $");
+#else
+extern const char *__progname;
+#define getprogname() __progname
+#endif
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <err.h>
+#include <errno.h>
+#include <stdbool.h>
+
+static inline bool
+match(const char *a, const char *b)
+{
+
+       return strncmp(a, b, strlen(b)) == 0;
+}
+
+int
+main(int argc, char *argv[])
+{
+       int s, e;
+       char *target;
+
+       if (argc != 2)
+               errx(EXIT_FAILURE, "invalid argument");
+       target = argv[1];
+
+       s = socket(AF_INET, SOCK_STREAM, 0);
+       if (s == -1)
+               err(EXIT_FAILURE, "socket");
+       e = shutdown(s, SHUT_RDWR);
+       if (e == -1)
+               err(EXIT_FAILURE, "shutdown");
+
+       if (match(target, "connect")) {
+               struct sockaddr_in sin;
+
+               memset(&sin, 0, sizeof(sin));
+               sin.sin_port = htons(31522);
+               sin.sin_addr.s_addr = inet_addr("127.0.0.1");
+               sin.sin_family = AF_INET;
+
+               e = connect(s, (struct sockaddr *)&sin, sizeof(sin));
+               if (e == 0)
+                       err(EXIT_FAILURE, "connect didn't fail on a shudown socket");
+               if (e == -1 && errno != EINVAL)
+                       err(EXIT_FAILURE, "connect failed with unexpected error");
+       } else if (match(target, "setsockopt")) {
+               int opt = 1;
+               e = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
+               if (e == 0)
+                       err(EXIT_FAILURE, "setsockopt didn't fail on a shutdown socket");
+               if (e == -1 && errno != ECONNRESET)
+                       err(EXIT_FAILURE, "setsockopt failed with unexpected error");
+       } else if (match(target, "getsockname")) {
+               struct sockaddr_storage ss;
+               socklen_t len;
+               e = getsockname(s, (struct sockaddr *)&ss, &len);
+               if (e == 0)
+                       err(EXIT_FAILURE, "getsockname didn't fail on a shutdown socket");
+               if (e == -1 && errno != EINVAL)
+                       err(EXIT_FAILURE, "getsockname failed with unexpected error");
+       } else if (match(target, "listen")) {
+               e = listen(s, 5);
+               if (e == 0)
+                       err(EXIT_FAILURE, "listen didn't fail on a shutdown socket");
+               if (e == -1 && errno != EINVAL)
+                       err(EXIT_FAILURE, "listen failed with unexpected error");
+       } else if (match(target, "bind")) {
+               struct sockaddr_in sin;
+
+               memset(&sin, 0, sizeof(sin));
+               sin.sin_port = htons(31522);
+               sin.sin_addr.s_addr = inet_addr("127.0.0.1");
+               sin.sin_family = AF_INET;
+
+               e = bind(s, (struct sockaddr *)&sin, sizeof(sin));
+               if (e == 0)
+                       err(EXIT_FAILURE, "bind didn't fail on a shutdown socket");
+               if (e == -1 && errno != EINVAL)
+                       err(EXIT_FAILURE, "bind failed with unexpected error");
+       } else if (match(target, "shutdown")) {



Home | Main Index | Thread Index | Old Index