Module Name: src Committed By: ozaki-r Date: Fri Nov 4 08:01:42 UTC 2022
Modified Files: src/distrib/sets/lists/debug: mi src/distrib/sets/lists/tests: mi src/etc/mtree: NetBSD.dist.tests src/tests/net/tcp: Makefile Added Files: src/tests/net/tcp: t_tcp_shutdown.sh tcp_shutdown.c Log Message: tests: add tests for invalid extra operations on a shutdown socket The tests cover some error paths that normally happen. To generate a diff of this commit: cvs rdiff -u -r1.389 -r1.390 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.1226 -r1.1227 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.194 -r1.195 src/etc/mtree/NetBSD.dist.tests cvs rdiff -u -r1.1 -r1.2 src/tests/net/tcp/Makefile cvs rdiff -u -r0 -r1.1 src/tests/net/tcp/t_tcp_shutdown.sh \ src/tests/net/tcp/tcp_shutdown.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.389 src/distrib/sets/lists/debug/mi:1.390 --- src/distrib/sets/lists/debug/mi:1.389 Sun Aug 28 07:30:41 2022 +++ src/distrib/sets/lists/debug/mi Fri Nov 4 08:01:42 2022 @@ -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 Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.1226 src/distrib/sets/lists/tests/mi:1.1227 --- src/distrib/sets/lists/tests/mi:1.1226 Fri Nov 4 07:54:27 2022 +++ src/distrib/sets/lists/tests/mi Fri Nov 4 08:01:42 2022 @@ -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 Index: src/etc/mtree/NetBSD.dist.tests diff -u src/etc/mtree/NetBSD.dist.tests:1.194 src/etc/mtree/NetBSD.dist.tests:1.195 --- src/etc/mtree/NetBSD.dist.tests:1.194 Wed Nov 2 09:37:56 2022 +++ src/etc/mtree/NetBSD.dist.tests Fri Nov 4 08:01:42 2022 @@ -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 Index: src/tests/net/tcp/Makefile diff -u src/tests/net/tcp/Makefile:1.1 src/tests/net/tcp/Makefile:1.2 --- src/tests/net/tcp/Makefile:1.1 Wed Nov 2 09:37:56 2022 +++ src/tests/net/tcp/Makefile Fri Nov 4 08:01:42 2022 @@ -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> Added files: Index: src/tests/net/tcp/t_tcp_shutdown.sh diff -u /dev/null src/tests/net/tcp/t_tcp_shutdown.sh:1.1 --- /dev/null Fri Nov 4 08:01:42 2022 +++ src/tests/net/tcp/t_tcp_shutdown.sh Fri Nov 4 08:01:42 2022 @@ -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 +} Index: src/tests/net/tcp/tcp_shutdown.c diff -u /dev/null src/tests/net/tcp/tcp_shutdown.c:1.1 --- /dev/null Fri Nov 4 08:01:42 2022 +++ src/tests/net/tcp/tcp_shutdown.c Fri Nov 4 08:01:42 2022 @@ -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")) { + e = shutdown(s, SHUT_RDWR); + if (e == 0) + err(EXIT_FAILURE, "shutdown didn't fail on a shutdown socket"); + if (e == -1 && errno != EINVAL) + err(EXIT_FAILURE, "shutdown failed with unexpected error"); + } else { + errx(EXIT_FAILURE, "unknown target: %s", target); + } + + e = close(s); + if (e == -1) + err(EXIT_FAILURE, "close"); + return 0; +}