glibc/sunrpc/tst-udp-garbage.c

105 lines
3.4 KiB
C
Raw Normal View History

/* Test that garbage packets do not affect timeout handling.
Copyright (C) 2017-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
#include <netinet/in.h>
#include <rpc/clnt.h>
#include <rpc/svc.h>
#include <stdbool.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/xsocket.h>
#include <support/xthread.h>
#include <sys/socket.h>
#include <unistd.h>
/* Descriptor for the server UDP socket. */
static int server_fd;
static void *
garbage_sender_thread (void *unused)
{
while (true)
{
struct sockaddr_storage sa;
socklen_t salen = sizeof (sa);
char buf[1];
if (recvfrom (server_fd, buf, sizeof (buf), 0,
(struct sockaddr *) &sa, &salen) < 0)
FAIL_EXIT1 ("recvfrom: %m");
/* Send garbage packets indefinitely. */
buf[0] = 0;
while (true)
{
/* sendto can fail if the client closed the socket. */
if (sendto (server_fd, buf, sizeof (buf), 0,
(struct sockaddr *) &sa, salen) < 0)
break;
/* Wait a bit, to avoid burning too many CPU cycles in a
tight loop. The wait period must be much shorter than
the client timeouts configured below. */
usleep (50 * 1000);
}
}
}
static int
do_test (void)
{
support_become_root ();
support_enter_network_namespace ();
server_fd = xsocket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
struct sockaddr_in server_address =
{
.sin_family = AF_INET,
.sin_addr.s_addr = htonl (INADDR_LOOPBACK),
};
xbind (server_fd,
(struct sockaddr *) &server_address, sizeof (server_address));
{
socklen_t sinlen = sizeof (server_address);
xgetsockname (server_fd, (struct sockaddr *) &server_address, &sinlen);
TEST_VERIFY (sizeof (server_address) == sinlen);
}
/* Garbage packet source. */
xpthread_detach (xpthread_create (NULL, garbage_sender_thread, NULL));
/* Test client. Use an arbitrary timeout of one second, which is
much longer than the garbage packet interval, but still
reasonably short, so that the test completes quickly. */
int client_fd = RPC_ANYSOCK;
CLIENT *clnt = clntudp_create (&server_address,
1, 2, /* Arbitrary RPC endpoint numbers. */
(struct timeval) { 1, 0 },
&client_fd);
if (clnt == NULL)
FAIL_EXIT1 ("clntudp_create: %m");
TEST_VERIFY (clnt_call (clnt, 3, /* Arbitrary RPC procedure number. */
(xdrproc_t) xdr_void, NULL,
(xdrproc_t) xdr_void, NULL,
((struct timeval) { 1, 0 })));
return 0;
}
#include <support/test-driver.c>