diff options
Diffstat (limited to 'lib/libc/tests/gen')
35 files changed, 4787 insertions, 0 deletions
diff --git a/lib/libc/tests/gen/Makefile b/lib/libc/tests/gen/Makefile new file mode 100644 index 000000000000..8c2151105209 --- /dev/null +++ b/lib/libc/tests/gen/Makefile @@ -0,0 +1,136 @@ +.include <bsd.own.mk> + +ATF_TESTS_C+= arc4random_test +ATF_TESTS_C+= dir2_test +ATF_TESTS_C+= dlopen_empty_test +ATF_TESTS_C+= fmtcheck2_test +ATF_TESTS_C+= fmtmsg_test +ATF_TESTS_C+= fnmatch2_test +ATF_TESTS_C+= fpclassify2_test +.if ${COMPILER_FEATURES:Mblocks} +ATF_TESTS_C+= fts_blocks_test +.endif +ATF_TESTS_C+= fts_misc_test +ATF_TESTS_C+= fts_options_test +ATF_TESTS_C+= ftw_test +ATF_TESTS_C+= getentropy_test +ATF_TESTS_C+= getmntinfo_test +ATF_TESTS_C+= glob2_test +.if ${COMPILER_FEATURES:Mblocks} +ATF_TESTS_C+= glob_blocks_test +.endif +ATF_TESTS_C+= makecontext_test +ATF_TESTS_C+= opendir_test +ATF_TESTS_C+= popen_test +ATF_TESTS_C+= posix_spawn_test +ATF_TESTS_C+= realpath2_test +ATF_TESTS_C+= scandir_test +.if ${COMPILER_FEATURES:Mblocks} +ATF_TESTS_C+= scandir_blocks_test +.endif +ATF_TESTS_C+= sig2str_test +ATF_TESTS_C+= sigsetops_test +ATF_TESTS_C+= wordexp_test + +# TODO: t_closefrom, t_fmtcheck, t_randomid, +# TODO: t_siginfo (fixes require further inspection) +# TODO: t_sethostname_test (consistently screws up the hostname) + +FILESGROUPS+= posix_spawn_test_FILES + +posix_spawn_test_FILES= spawnp_enoexec.sh +posix_spawn_test_FILESDIR= ${TESTSDIR} +posix_spawn_test_FILESMODE= 0755 +posix_spawn_test_FILESOWN= root +posix_spawn_test_FILESGRP= wheel +posix_spawn_test_FILESPACKAGE= ${PACKAGE} + +CFLAGS+= -DTEST_LONG_DOUBLE + +# Define __HAVE_LONG_DOUBLE for architectures whose long double has greater +# precision than their double. +.if ${MACHINE_CPUARCH} == "aarch64" || \ + ${MACHINE_CPUARCH} == "amd64" || \ + ${MACHINE_CPUARCH} == "i386" || \ + ${MACHINE_CPUARCH} == "riscv" +CFLAGS+= -D__HAVE_LONG_DOUBLE +.endif + +NETBSD_ATF_TESTS_C= alarm_test +NETBSD_ATF_TESTS_C+= assert_test +NETBSD_ATF_TESTS_C+= basedirname_test +NETBSD_ATF_TESTS_C+= cpuset_test +NETBSD_ATF_TESTS_C+= dir_test +NETBSD_ATF_TESTS_C+= floatunditf_test +NETBSD_ATF_TESTS_C+= fnmatch_test +NETBSD_ATF_TESTS_C+= fpclassify_test +NETBSD_ATF_TESTS_C+= fpsetmask_test +NETBSD_ATF_TESTS_C+= fpsetround_test +NETBSD_ATF_TESTS_C+= ftok_test +NETBSD_ATF_TESTS_C+= getcwd_test +NETBSD_ATF_TESTS_C+= getgrent_test +NETBSD_ATF_TESTS_C+= glob_test +NETBSD_ATF_TESTS_C+= humanize_number_test +NETBSD_ATF_TESTS_C+= isnan_test +NETBSD_ATF_TESTS_C+= nice_test +NETBSD_ATF_TESTS_C+= pause_test +NETBSD_ATF_TESTS_C+= raise_test +NETBSD_ATF_TESTS_C+= realpath_test +NETBSD_ATF_TESTS_C+= setdomainname_test +NETBSD_ATF_TESTS_C+= sethostname_test +NETBSD_ATF_TESTS_C+= sleep_test +NETBSD_ATF_TESTS_C+= syslog_test +NETBSD_ATF_TESTS_C+= time_test +NETBSD_ATF_TESTS_C+= ttyname_test +NETBSD_ATF_TESTS_C+= vis_test + +.include "../Makefile.netbsd-tests" + +CFLAGS.getentropy_test+= -I${SRCTOP}/include +LIBADD.getentropy_test+= c +LIBADD.humanize_number_test+= util + +LIBADD.fpclassify_test+=m +LIBADD.fpsetround_test+=m +LIBADD.siginfo_test+= m + +LIBADD.nice_test+= pthread +LIBADD.syslog_test+= pthread + +CFLAGS+= -I${.CURDIR} + +SRCS.fmtcheck2_test= fmtcheck_test.c +SRCS.fnmatch2_test= fnmatch_test.c + +TEST_METADATA.setdomainname_test+= is_exclusive=true +TESTS_SUBDIRS= execve +TESTS_SUBDIRS+= posix_spawn + +# Tests that require address sanitizer +.if ${COMPILER_FEATURES:Masan} +.for t in scandir_test realpath2_test +CFLAGS.${t}.c+= -fsanitize=address +LDFLAGS.${t}+= -fsanitize=address +.endfor +.endif + +# Tests that require blocks support +.for t in fts_blocks_test glob_blocks_test scandir_blocks_test +CFLAGS.${t}.c+= -fblocks +LIBADD.${t}+= BlocksRuntime +.endfor + +# The old testcase name +TEST_FNMATCH= test-fnmatch +CLEANFILES+= ${GEN_SH_CASE_TESTCASES} +sh-tests: .PHONY +.for target in clean obj depend all + @cd ${.CURDIR} && ${MAKE} PROG=${TEST_FNMATCH} \ + -DNO_SUBDIR ${target} +.endfor + @cd ${.OBJDIR} && ./${TEST_FNMATCH} -s 1 > \ + ${SRCTOP}/bin/sh/tests/builtins/case2.0 + @cd ${.OBJDIR} && ./${TEST_FNMATCH} -s 2 > \ + ${SRCTOP}/bin/sh/tests/builtins/case3.0 + +.include <bsd.test.mk> diff --git a/lib/libc/tests/gen/Makefile.depend b/lib/libc/tests/gen/Makefile.depend new file mode 100644 index 000000000000..33d10e940cd0 --- /dev/null +++ b/lib/libc/tests/gen/Makefile.depend @@ -0,0 +1,21 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/atf/libatf-c \ + lib/libc \ + lib/libcompiler_rt \ + lib/libnetbsd \ + lib/libthr \ + lib/libutil \ + lib/msun \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/lib/libc/tests/gen/arc4random_test.c b/lib/libc/tests/gen/arc4random_test.c new file mode 100644 index 000000000000..79139f29f8fa --- /dev/null +++ b/lib/libc/tests/gen/arc4random_test.c @@ -0,0 +1,112 @@ +/*- + * Copyright (c) 2011 David Schultz + * Copyright (c) 2024 Robert Clausecker <fuz@FreeBSD.org> + * 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 AUTHOR 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 AUTHOR 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/types.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <errno.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +/* + * BUFSIZE is the number of bytes of rc4 output to compare. The probability + * that this test fails spuriously is 2**(-BUFSIZE * 8). + */ +#define BUFSIZE 8 + +/* + * Test whether arc4random_buf() returns the same sequence of bytes in both + * parent and child processes. (Hint: It shouldn't.) + */ +ATF_TC_WITHOUT_HEAD(test_arc4random); +ATF_TC_BODY(test_arc4random, tc) +{ + struct shared_page { + char parentbuf[BUFSIZE]; + char childbuf[BUFSIZE]; + } *page; + pid_t pid; + char c; + + page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0); + ATF_REQUIRE_MSG(page != MAP_FAILED, "mmap failed; errno=%d", errno); + + arc4random_buf(&c, 1); + + pid = fork(); + ATF_REQUIRE(0 <= pid); + if (pid == 0) { + /* child */ + arc4random_buf(page->childbuf, BUFSIZE); + exit(0); + } else { + /* parent */ + int status; + arc4random_buf(page->parentbuf, BUFSIZE); + wait(&status); + } + ATF_CHECK_MSG(memcmp(page->parentbuf, page->childbuf, BUFSIZE) != 0, + "sequences are the same"); +} + +/* + * Test whether arc4random_uniform() returns a number below the given threshold. + * Test with various thresholds. + */ +ATF_TC_WITHOUT_HEAD(test_arc4random_uniform); +ATF_TC_BODY(test_arc4random_uniform, tc) +{ + size_t i, j; + static const uint32_t thresholds[] = { + 1, 2, 3, 4, 5, 10, 100, 1000, + INT32_MAX, (uint32_t)INT32_MAX + 1, + UINT32_MAX - 1000000000, UINT32_MAX - 1000000, UINT32_MAX - 1, 0 + }; + + for (i = 0; thresholds[i] != 0; i++) + for (j = 0; j < 10000; j++) + ATF_CHECK(arc4random_uniform(thresholds[i]) < thresholds[i]); + + /* for a threshold of zero, just check that we get zero every time */ + for (j = 0; j < 1000; j++) + ATF_CHECK_EQ(0, arc4random_uniform(0)); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, test_arc4random); + ATF_TP_ADD_TC(tp, test_arc4random_uniform); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/dir2_test.c b/lib/libc/tests/gen/dir2_test.c new file mode 100644 index 000000000000..4ec5a1759d06 --- /dev/null +++ b/lib/libc/tests/gen/dir2_test.c @@ -0,0 +1,186 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2017 Spectra Logic Corporation + * 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 AUTHOR 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 AUTHOR 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. + */ + +/* + * Test cases for operations on DIR objects: + * opendir, readdir, seekdir, telldir, closedir, etc + */ + +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +ATF_TC(telldir_after_seekdir); +ATF_TC_HEAD(telldir_after_seekdir, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after seekdir(3) " + "should return the argument passed to seekdir."); +} +ATF_TC_BODY(telldir_after_seekdir, tc) +{ + const int NUMFILES = 1000; + char template[] = "dXXXXXX"; + char *tmpdir; + int i, dirfd; + DIR *dirp; + struct dirent *de; + long beginning, middle, end, td; + + /* Create a temporary directory */ + tmpdir = mkdtemp(template); + ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed"); + dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY); + ATF_REQUIRE(dirfd > 0); + + /* + * Fill it with files. Must be > 128 to ensure that the directory + * can't fit within a single page + */ + for (i = 0; i < NUMFILES; i = i+1) { + int fd; + char filename[16]; + + snprintf(filename, sizeof(filename), "%d", i); + fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600); + ATF_REQUIRE(fd > 0); + close(fd); + } + + /* Get some directory bookmarks in various locations */ + dirp = fdopendir(dirfd); + ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed"); + beginning = telldir(dirp); + for (i = 0; i < NUMFILES / 2; i = i+1) { + de = readdir(dirp); + ATF_REQUIRE_MSG(de != NULL, "readdir failed"); + } + middle = telldir(dirp); + for (; i < NUMFILES - 1; i = i+1) { + de = readdir(dirp); + ATF_REQUIRE_MSG(de != NULL, "readdir failed"); + } + end = telldir(dirp); + + /* + * Seekdir to each bookmark, check the telldir after seekdir condition, + * and check that the bookmark is valid by reading another directory + * entry. + */ + + seekdir(dirp, beginning); + td = telldir(dirp); + ATF_CHECK_EQ(beginning, td); + ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index"); + + seekdir(dirp, middle); + td = telldir(dirp); + ATF_CHECK_EQ(middle, td); + ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index"); + + seekdir(dirp, end); + td = telldir(dirp); + ATF_CHECK_EQ(end, td); + ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index"); + + closedir(dirp); +} + +ATF_TC(telldir_at_end_of_block); +ATF_TC_HEAD(telldir_at_end_of_block, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after readdir(3) read the last entry in the block should return a valid location"); +} +ATF_TC_BODY(telldir_at_end_of_block, tc) +{ + /* For UFS and ZFS, blocks roll over at 128 directory entries. */ + const int NUMFILES = 129; + char template[] = "dXXXXXX"; + char *tmpdir; + int i, dirfd; + DIR *dirp; + struct dirent *de; + long td; + char last_filename[16]; + + /* Create a temporary directory */ + tmpdir = mkdtemp(template); + ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed"); + dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY); + ATF_REQUIRE(dirfd > 0); + + /* + * Fill it with files. Must be > 128 to ensure that the directory + * can't fit within a single page. The "-2" accounts for "." and ".." + */ + for (i = 0; i < NUMFILES - 2; i = i+1) { + int fd; + char filename[16]; + + snprintf(filename, sizeof(filename), "%d", i); + fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600); + ATF_REQUIRE(fd > 0); + close(fd); + } + + /* Read all entries within the first page */ + dirp = fdopendir(dirfd); + ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed"); + for (i = 0; i < NUMFILES - 1; i = i + 1) + ATF_REQUIRE_MSG(readdir(dirp) != NULL, "readdir failed"); + + /* Call telldir at the end of a page */ + td = telldir(dirp); + + /* Read the last entry */ + de = readdir(dirp); + ATF_REQUIRE_MSG(de != NULL, "readdir failed"); + strlcpy(last_filename, de->d_name, sizeof(last_filename)); + + /* Seek back to the bookmark. readdir() should return the last entry */ + seekdir(dirp, td); + de = readdir(dirp); + ATF_REQUIRE_STREQ_MSG(last_filename, de->d_name, + "seekdir went to the wrong directory position"); + + closedir(dirp); +} + + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, telldir_after_seekdir); + ATF_TP_ADD_TC(tp, telldir_at_end_of_block); + + return atf_no_error(); +} diff --git a/lib/libc/tests/gen/dlopen_empty_test.c b/lib/libc/tests/gen/dlopen_empty_test.c new file mode 100644 index 000000000000..6fb3bf8d8343 --- /dev/null +++ b/lib/libc/tests/gen/dlopen_empty_test.c @@ -0,0 +1,94 @@ +/*- + * Copyright (c) 2016 Maksym Sobolyev + * 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 AUTHOR 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 AUTHOR 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/stat.h> +#include <dlfcn.h> +#include <errno.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <atf-c.h> + +static const char *funname; +static char *soname; + +static void +sigsegv_handler(int sig __unused) +{ + unlink(soname); + free(soname); + atf_tc_fail("got SIGSEGV in the %s(3)", funname); +} + +ATF_TC(dlopen_empty_test); +ATF_TC_HEAD(dlopen_empty_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Tests the dlopen() of an empty file " + "returns an error"); +} +ATF_TC_BODY(dlopen_empty_test, tc) +{ + char tempname[] = "/tmp/temp.XXXXXX"; + char *fname; + int fd; + void *dlh; + struct sigaction act, oact; + + fname = mktemp(tempname); + ATF_REQUIRE_MSG(fname != NULL, "mktemp failed; errno=%d", errno); + asprintf(&soname, "%s.so", fname); + ATF_REQUIRE_MSG(soname != NULL, "asprintf failed; errno=%d", ENOMEM); + fd = open(soname, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE); + ATF_REQUIRE_MSG(fd != -1, "open(\"%s\") failed; errno=%d", soname, errno); + close(fd); + + act.sa_handler = sigsegv_handler; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + ATF_CHECK_MSG(sigaction(SIGSEGV, &act, &oact) != -1, + "sigaction() failed"); + + funname = "dlopen"; + dlh = dlopen(soname, RTLD_LAZY); + if (dlh != NULL) { + funname = "dlclose"; + dlclose(dlh); + } + ATF_REQUIRE_MSG(dlh == NULL, "dlopen(\"%s\") did not fail", soname); + unlink(soname); + free(soname); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, dlopen_empty_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/execve/Makefile b/lib/libc/tests/gen/execve/Makefile new file mode 100644 index 000000000000..7bf911d1cb47 --- /dev/null +++ b/lib/libc/tests/gen/execve/Makefile @@ -0,0 +1,7 @@ +.include <bsd.own.mk> + +NETBSD_ATF_TESTS_C= execve_test + +.include "../../Makefile.netbsd-tests" + +.include <bsd.test.mk> diff --git a/lib/libc/tests/gen/execve/Makefile.depend b/lib/libc/tests/gen/execve/Makefile.depend new file mode 100644 index 000000000000..e89a5c52c82a --- /dev/null +++ b/lib/libc/tests/gen/execve/Makefile.depend @@ -0,0 +1,18 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/atf/libatf-c \ + lib/libc \ + lib/libcompiler_rt \ + lib/libnetbsd \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/lib/libc/tests/gen/fmtcheck_test.c b/lib/libc/tests/gen/fmtcheck_test.c new file mode 100644 index 000000000000..27a12217e81e --- /dev/null +++ b/lib/libc/tests/gen/fmtcheck_test.c @@ -0,0 +1,102 @@ +/* $NetBSD: tfmtcheck.c,v 1.3 2008/04/28 20:23:04 martin Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code was contributed to The NetBSD Foundation by Allen Briggs. + * + * 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/param.h> +#include <err.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +struct test_fmt { + char *fmt1; + char *fmt2; + int correct; +} test_fmts[] = { + { "%d", "%d", 1 }, + { "%2d", "%2.2d", 1 }, + { "%x", "%d", 1 }, + { "%u", "%d", 1 }, + { "%03d", "%d", 1 }, + { "%-2d", "%d", 1 }, + { "%d", "%-12.1d", 1 }, + { "%d", "%-01.3d", 1 }, + { "%X", "%-01.3d", 1 }, + { "%D", "%ld", 1 }, + { "%s", "%s", 1 }, + { "%s", "This is a %s test", 1 }, + { "Hi, there. This is a %s test", "%s", 1 }, + { "%d", "%s", 2 }, + { "%e", "%s", 2 }, + { "%r", "%d", 2 }, + { "%*.2d", "%*d", 1 }, + { "%2.*d", "%*d", 2 }, + { "%*d", "%*d", 1 }, + { "%-3", "%d", 2 }, + { "%d %s", "%d", 2 }, + { "%*.*.*d", "%*.*.*d", 2 }, + { "%d", "%d %s", 1 }, + { "%40s", "%20s", 1 }, + { "%x %x %x", "%o %u %d", 1 }, + { "%o %u %d", "%x %x %X", 1 }, + { "%#o %u %#-d", "%x %#x %X", 1 }, + { "%qd", "%llx", 1 }, + { "%%", "%llx", 1 }, + { "%p %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 }, +}; + +ATF_TC_WITHOUT_HEAD(fmtcheck_test); +ATF_TC_BODY(fmtcheck_test, tc) +{ + int i; + const char *f, *cf, *f1, *f2; + + for (i = 0; i < nitems(test_fmts); i++) { + f1 = test_fmts[i].fmt1; + f2 = test_fmts[i].fmt2; + f = fmtcheck(f1, f2); + if (test_fmts[i].correct == 1) + cf = f1; + else + cf = f2; + ATF_CHECK_MSG(f == cf, + "Test %d: (%s) vs. (%s) failed " + "(should have returned %s)", i + 1, f1, f2, + (test_fmts[i].correct == 1) ? "1st" : "2nd"); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, fmtcheck_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fmtmsg_test.c b/lib/libc/tests/gen/fmtmsg_test.c new file mode 100644 index 000000000000..30a5156cdcc8 --- /dev/null +++ b/lib/libc/tests/gen/fmtmsg_test.c @@ -0,0 +1,249 @@ +/*- + * Copyright (c) 2012 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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/param.h> +#include <sys/wait.h> +#include <err.h> +#include <errno.h> +#include <fmtmsg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +static char *run_test(long classification, const char *label, int severity, + const char *text, const char *action, const char *tag); + +struct testcase { + long classification; + const char *label; + int severity; + const char *text; + const char *action; + const char *tag; + const char *msgverb; + const char *result; +} testcases[] = { + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + NULL, + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "text:severity:action:tag", + "illegal option -- z: ERROR\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "text", + "illegal option -- z\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "severity:text", + "ERROR: illegal option -- z\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "ignore me", + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "tag:severity:text:nothing:action", + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "", + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, MM_NULLLBL, MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + NULL, + "ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", MM_NULLACT, MM_NULLTAG, + NULL, + "BSD:ls: ERROR: illegal option -- z\n" + }, + { + MM_UTIL | MM_NULLMC, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + NULL, + "" + }, + { + MM_APPL | MM_PRINT, "ABCDEFGHIJ:abcdefghijklmn", MM_INFO, + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "refer to manual", "ABCDEFGHIJ:abcdefghijklmn:001", + NULL, + "ABCDEFGHIJ:abcdefghijklmn: INFO: " + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" + "TO FIX: refer to manual ABCDEFGHIJ:abcdefghijklmn:001\n" + }, + { + MM_OPSYS | MM_PRINT, "TEST:test", MM_HALT, + "failed", "nothing can help me", "NOTHING", + NULL, + "TEST:test: HALT: failed\n" + "TO FIX: nothing can help me NOTHING\n" + }, + { + MM_OPSYS | MM_PRINT, "TEST:test", MM_WARNING, + "failed", "nothing can help me", "NOTHING", + NULL, + "TEST:test: WARNING: failed\n" + "TO FIX: nothing can help me NOTHING\n" + }, + { + MM_OPSYS | MM_PRINT, "TEST:test", MM_NOSEV, + "failed", "nothing can help me", "NOTHING", + NULL, + "TEST:test: failed\n" + "TO FIX: nothing can help me NOTHING\n" + } +}; + +static char * +run_test(long classification, const char *label, int severity, + const char *text, const char *action, const char *tag) +{ + int pip[2]; + pid_t pid, wpid; + char *result, *p; + size_t resultsize; + ssize_t n; + int status; + + if (pipe(pip) == -1) + err(2, "pipe"); + pid = fork(); + if (pid == -1) + err(2, "fork"); + if (pid == 0) { + close(pip[0]); + if (pip[1] != STDERR_FILENO && + dup2(pip[1], STDERR_FILENO) == -1) + _exit(2); + if (fmtmsg(classification, label, severity, text, action, tag) + != MM_OK) + _exit(1); + else + _exit(0); + } + close(pip[1]); + resultsize = 1024; + result = malloc(resultsize); + p = result; + while ((n = read(pip[0], p, result + resultsize - p - 1)) != 0) { + if (n == -1) { + if (errno == EINTR) + continue; + else + err(2, "read"); + } + p += n; + if (result + resultsize == p - 1) { + resultsize *= 2; + result = realloc(result, resultsize); + if (result == NULL) + err(2, "realloc"); + } + } + if (memchr(result, '\0', p - result) != NULL) { + free(result); + return (NULL); + } + *p = '\0'; + close(pip[0]); + while ((wpid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) + ; + if (wpid == -1) + err(2, "waitpid"); + if (status != 0) { + free(result); + return (NULL); + } + return (result); +} + +ATF_TC_WITHOUT_HEAD(fmtmsg_test); +ATF_TC_BODY(fmtmsg_test, tc) +{ + char *result; + struct testcase *t; + int i; + + for (i = 0; i < nitems(testcases); i++) { + t = &testcases[i]; + if (t->msgverb != NULL) + setenv("MSGVERB", t->msgverb, 1); + else + unsetenv("MSGVERB"); + result = run_test(t->classification, t->label, t->severity, + t->text, t->action, t->tag); + ATF_CHECK_MSG(result != NULL, "testcase %d failed", i + 1); + if (result != NULL) + ATF_CHECK_MSG(strcmp(result, t->result) == 0, + "results for testcase %d didn't match; " + "`%s` != `%s`", i + 1, result, t->result); + free(result); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, fmtmsg_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fnmatch_test.c b/lib/libc/tests/gen/fnmatch_test.c new file mode 100644 index 000000000000..0ff7400a4a4f --- /dev/null +++ b/lib/libc/tests/gen/fnmatch_test.c @@ -0,0 +1,266 @@ +/*- + * Copyright (c) 2010 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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/param.h> +#include <errno.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "fnmatch_testcases.h" + +static const char * +flags_to_string(int flags) +{ + static const int flagvalues[] = { FNM_NOESCAPE, FNM_PATHNAME, + FNM_PERIOD, FNM_LEADING_DIR, FNM_CASEFOLD, 0 }; + static const char flagnames[] = "FNM_NOESCAPE\0FNM_PATHNAME\0FNM_PERIOD\0FNM_LEADING_DIR\0FNM_CASEFOLD\0"; + static char result[sizeof(flagnames) + 3 * sizeof(int) + 2]; + char *p; + size_t i, len; + const char *fp; + + p = result; + fp = flagnames; + for (i = 0; flagvalues[i] != 0; i++) { + len = strlen(fp); + if (flags & flagvalues[i]) { + if (p != result) + *p++ = '|'; + memcpy(p, fp, len); + p += len; + flags &= ~flagvalues[i]; + } + fp += len + 1; + } + if (p == result) + memcpy(p, "0", 2); + else if (flags != 0) + sprintf(p, "%d", flags); + else + *p = '\0'; + return result; +} + +ATF_TC_WITHOUT_HEAD(fnmatch_test); +ATF_TC_BODY(fnmatch_test, tc) +{ + size_t i; + int flags, result; + struct testcase *t; + + for (i = 0; i < nitems(testcases); i++) { + t = &testcases[i]; + flags = t->flags; + do { + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + if (strchr(t->pattern, '\\') == NULL && + !(flags & FNM_NOESCAPE)) { + flags |= FNM_NOESCAPE; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (strchr(t->pattern, '\\') != NULL && + strchr(t->string, '\\') == NULL && + t->result == FNM_NOMATCH && + !(flags & (FNM_NOESCAPE | FNM_LEADING_DIR))) { + flags |= FNM_NOESCAPE; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((t->string[0] != '.' || t->pattern[0] == '.' || + t->result == FNM_NOMATCH) && + !(flags & (FNM_PATHNAME | FNM_PERIOD))) { + flags |= FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((strchr(t->string, '/') == NULL || + t->result == FNM_NOMATCH) && + !(flags & FNM_PATHNAME)) { + flags |= FNM_PATHNAME; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((((t->string[0] != '.' || t->pattern[0] == '.') && + strstr(t->string, "/.") == NULL) || + t->result == FNM_NOMATCH) && + flags & FNM_PATHNAME && !(flags & FNM_PERIOD)) { + flags |= FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((((t->string[0] != '.' || t->pattern[0] == '.') && + strchr(t->string, '/') == NULL) || + t->result == FNM_NOMATCH) && + !(flags & (FNM_PATHNAME | FNM_PERIOD))) { + flags |= FNM_PATHNAME | FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((strchr(t->string, '/') == NULL || t->result == 0) + && !(flags & FNM_LEADING_DIR)) { + flags |= FNM_LEADING_DIR; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (t->result == 0 && !(flags & FNM_CASEFOLD)) { + flags |= FNM_CASEFOLD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (strchr(t->pattern, '\\') == NULL && + t->result == 0 && + !(flags & (FNM_NOESCAPE | FNM_CASEFOLD))) { + flags |= FNM_NOESCAPE | FNM_CASEFOLD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + } while (0); + + ATF_CHECK(result == t->result); + if (result == t->result) + printf("fnmatch(\"%s\", \"%s\", %s) == %d\n", + t->pattern, t->string, flags_to_string(flags), result); + else + printf("fnmatch(\"%s\", \"%s\", %s) != %d (was %d)\n", + t->pattern, t->string, flags_to_string(flags), + t->result, result); + } + +} + +ATF_TC(fnmatch_characterclass); +ATF_TC_HEAD(fnmatch_characterclass, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test fnmatch with character classes"); +} + +ATF_TC_BODY(fnmatch_characterclass, tc) +{ + ATF_CHECK(fnmatch("[[:alnum:]]", "a", 0) == 0); + ATF_CHECK(fnmatch("[[:cntrl:]]", "\a", 0) == 0); + ATF_CHECK(fnmatch("[[:lower:]]", "a", 0) == 0); + ATF_CHECK(fnmatch("[[:space:]]", " ", 0) == 0); + ATF_CHECK(fnmatch("[[:alpha:]]", "a", 0) == 0); + ATF_CHECK(fnmatch("[[:digit:]]", "0", 0) == 0); + ATF_CHECK(fnmatch("[[:print:]]", "a", 0) == 0); + ATF_CHECK(fnmatch("[[:upper:]]", "A", 0) == 0); + ATF_CHECK(fnmatch("[[:blank:]]", " ", 0) == 0); + ATF_CHECK(fnmatch("[[:graph:]]", "a", 0) == 0); + ATF_CHECK(fnmatch("[[:punct:]]", ".", 0) == 0); + ATF_CHECK(fnmatch("[[:xdigit:]]", "f", 0) == 0); + + /* + * POSIX.1, section 9.3.5. states that '[:' and ':]' + * should be interpreted as character classes symbol only + * when part of a bracket expression. + */ + ATF_CHECK(fnmatch("[:alnum:]", "a", 0) == 0); + ATF_CHECK(fnmatch("[:alnum:]", ":", 0) == 0); + ATF_CHECK(fnmatch("[:alnum:]", "1", 0) != 0); +} + +ATF_TC(fnmatch_collsym); +ATF_TC_HEAD(fnmatch_collsym, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test fnmatch with collating symbols"); +} + +ATF_TC_BODY(fnmatch_collsym, tc) +{ + setlocale(LC_ALL, "cs_CZ.UTF-8"); + ATF_CHECK(fnmatch("[ch]", "ch", 0) != 0); + ATF_CHECK(fnmatch("[[.ch.]]", "ch", 0) == 0); + ATF_CHECK(fnmatch("[[.ch.]]h", "chh", 0) == 0); + + /* + * POSIX.1, section 9.3.5. states that '[.' and '.]' + * should be interpreted as a collating symbol only + * when part of a bracket expression. + */ + ATF_CHECK(fnmatch("[.ch.]", "c", 0) == 0); + ATF_CHECK(fnmatch("[.ch.]", "h", 0) == 0); + ATF_CHECK(fnmatch("[.ch.]", ".", 0) == 0); +} + +ATF_TC(fnmatch_equivclass); +ATF_TC_HEAD(fnmatch_equivclass, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test fnmatch with equivalence classes"); +} + +ATF_TC_BODY(fnmatch_equivclass, tc) +{ + setlocale(LC_ALL, "en_US.UTF-8"); + ATF_CHECK(fnmatch("[[=a=]]b", "ab", 0) == 0); + ATF_CHECK(fnmatch("[[=a=]]b", "Ab", 0) == 0); + ATF_CHECK(fnmatch("[[=à =]]b", "ab", 0) == 0); + ATF_CHECK(fnmatch("[[=a=]]b", "à b", 0) == 0); + + /* + * POSIX.1, section 9.3.5. states that '[=' and '=]' + * should be interpreted as an equivalence class only + * when part of a bracket expression. + */ + ATF_CHECK(fnmatch("[=a=]b", "=b", 0) == 0); + ATF_CHECK(fnmatch("[=a=]b", "ab", 0) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, fnmatch_test); + ATF_TP_ADD_TC(tp, fnmatch_collsym); + ATF_TP_ADD_TC(tp, fnmatch_characterclass); + ATF_TP_ADD_TC(tp, fnmatch_equivclass); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fnmatch_testcases.h b/lib/libc/tests/gen/fnmatch_testcases.h new file mode 100644 index 000000000000..196160a4801b --- /dev/null +++ b/lib/libc/tests/gen/fnmatch_testcases.h @@ -0,0 +1,173 @@ +/*- + * Copyright (c) 2010 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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 <fnmatch.h> + +struct testcase { + const char *pattern; + const char *string; + int flags; + int result; +} testcases[] = { + { "", "", 0, 0 }, + { "a", "a", 0, 0 }, + { "a", "b", 0, FNM_NOMATCH }, + { "a", "A", 0, FNM_NOMATCH }, + { "*", "a", 0, 0 }, + { "*", "aa", 0, 0 }, + { "*a", "a", 0, 0 }, + { "*a", "b", 0, FNM_NOMATCH }, + { "*a*", "b", 0, FNM_NOMATCH }, + { "*a*b*", "ab", 0, 0 }, + { "*a*b*", "qaqbq", 0, 0 }, + { "*a*bb*", "qaqbqbbq", 0, 0 }, + { "*a*bc*", "qaqbqbcq", 0, 0 }, + { "*a*bb*", "qaqbqbb", 0, 0 }, + { "*a*bc*", "qaqbqbc", 0, 0 }, + { "*a*bb", "qaqbqbb", 0, 0 }, + { "*a*bc", "qaqbqbc", 0, 0 }, + { "*a*bb", "qaqbqbbq", 0, FNM_NOMATCH }, + { "*a*bc", "qaqbqbcq", 0, FNM_NOMATCH }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaa", 0, FNM_NOMATCH }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaa", 0, 0 }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaaa", 0, 0 }, + { ".*.*.*.*.*.*.*.*.*.*", ".........", 0, FNM_NOMATCH }, + { ".*.*.*.*.*.*.*.*.*.*", "..........", 0, 0 }, + { ".*.*.*.*.*.*.*.*.*.*", "...........", 0, 0 }, + { "*?*?*?*?*?*?*?*?*?*?*", "123456789", 0, FNM_NOMATCH }, + { "??????????*", "123456789", 0, FNM_NOMATCH }, + { "*??????????", "123456789", 0, FNM_NOMATCH }, + { "*?*?*?*?*?*?*?*?*?*?*", "1234567890", 0, 0 }, + { "??????????*", "1234567890", 0, 0 }, + { "*??????????", "1234567890", 0, 0 }, + { "*?*?*?*?*?*?*?*?*?*?*", "12345678901", 0, 0 }, + { "??????????*", "12345678901", 0, 0 }, + { "*??????????", "12345678901", 0, 0 }, + { "[x]", "x", 0, 0 }, + { "[*]", "*", 0, 0 }, + { "[?]", "?", 0, 0 }, + { "[", "[", 0, 0 }, + { "[[]", "[", 0, 0 }, + { "[[]", "x", 0, FNM_NOMATCH }, + { "[*]", "", 0, FNM_NOMATCH }, + { "[*]", "x", 0, FNM_NOMATCH }, + { "[?]", "x", 0, FNM_NOMATCH }, + { "*[*]*", "foo*foo", 0, 0 }, + { "*[*]*", "foo", 0, FNM_NOMATCH }, + { "[0-9]", "0", 0, 0 }, + { "[0-9]", "5", 0, 0 }, + { "[0-9]", "9", 0, 0 }, + { "[0-9]", "/", 0, FNM_NOMATCH }, + { "[0-9]", ":", 0, FNM_NOMATCH }, + { "[0-9]", "*", 0, FNM_NOMATCH }, + { "[!0-9]", "0", 0, FNM_NOMATCH }, + { "[!0-9]", "5", 0, FNM_NOMATCH }, + { "[!0-9]", "9", 0, FNM_NOMATCH }, + { "[!0-9]", "/", 0, 0 }, + { "[!0-9]", ":", 0, 0 }, + { "[!0-9]", "*", 0, 0 }, + { "*[0-9]", "a0", 0, 0 }, + { "*[0-9]", "a5", 0, 0 }, + { "*[0-9]", "a9", 0, 0 }, + { "*[0-9]", "a/", 0, FNM_NOMATCH }, + { "*[0-9]", "a:", 0, FNM_NOMATCH }, + { "*[0-9]", "a*", 0, FNM_NOMATCH }, + { "*[!0-9]", "a0", 0, FNM_NOMATCH }, + { "*[!0-9]", "a5", 0, FNM_NOMATCH }, + { "*[!0-9]", "a9", 0, FNM_NOMATCH }, + { "*[!0-9]", "a/", 0, 0 }, + { "*[!0-9]", "a:", 0, 0 }, + { "*[!0-9]", "a*", 0, 0 }, + { "*[0-9]", "a00", 0, 0 }, + { "*[0-9]", "a55", 0, 0 }, + { "*[0-9]", "a99", 0, 0 }, + { "*[0-9]", "a0a0", 0, 0 }, + { "*[0-9]", "a5a5", 0, 0 }, + { "*[0-9]", "a9a9", 0, 0 }, + { "\\*", "*", 0, 0 }, + { "\\?", "?", 0, 0 }, + { "\\[x]", "[x]", 0, 0 }, + { "\\[", "[", 0, 0 }, + { "\\\\", "\\", 0, 0 }, + { "*\\**", "foo*foo", 0, 0 }, + { "*\\**", "foo", 0, FNM_NOMATCH }, + { "*\\\\*", "foo\\foo", 0, 0 }, + { "*\\\\*", "foo", 0, FNM_NOMATCH }, + { "\\(", "(", 0, 0 }, + { "\\a", "a", 0, 0 }, + { "\\*", "a", 0, FNM_NOMATCH }, + { "\\?", "a", 0, FNM_NOMATCH }, + { "\\*", "\\*", 0, FNM_NOMATCH }, + { "\\?", "\\?", 0, FNM_NOMATCH }, + { "\\[x]", "\\[x]", 0, FNM_NOMATCH }, + { "\\[x]", "\\x", 0, FNM_NOMATCH }, + { "\\[", "\\[", 0, FNM_NOMATCH }, + { "\\(", "\\(", 0, FNM_NOMATCH }, + { "\\a", "\\a", 0, FNM_NOMATCH }, + { "\\", "\\", 0, FNM_NOMATCH }, + { "\\", "", 0, FNM_NOMATCH }, + { "\\*", "\\*", FNM_NOESCAPE, 0 }, + { "\\?", "\\?", FNM_NOESCAPE, 0 }, + { "\\", "\\", FNM_NOESCAPE, 0 }, + { "\\\\", "\\", FNM_NOESCAPE, FNM_NOMATCH }, + { "\\\\", "\\\\", FNM_NOESCAPE, 0 }, + { "*\\*", "foo\\foo", FNM_NOESCAPE, 0 }, + { "*\\*", "foo", FNM_NOESCAPE, FNM_NOMATCH }, + { "*", ".", FNM_PERIOD, FNM_NOMATCH }, + { "?", ".", FNM_PERIOD, FNM_NOMATCH }, + { ".*", ".", 0, 0 }, + { ".*", "..", 0, 0 }, + { ".*", ".a", 0, 0 }, + { "[0-9]", ".", FNM_PERIOD, FNM_NOMATCH }, + { "a*", "a.", 0, 0 }, + { "a/a", "a/a", FNM_PATHNAME, 0 }, + { "a/*", "a/a", FNM_PATHNAME, 0 }, + { "*/a", "a/a", FNM_PATHNAME, 0 }, + { "*/*", "a/a", FNM_PATHNAME, 0 }, + { "a*b/*", "abbb/x", FNM_PATHNAME, 0 }, + { "a*b/*", "abbb/.x", FNM_PATHNAME, 0 }, + { "*", "a/a", FNM_PATHNAME, FNM_NOMATCH }, + { "*/*", "a/a/a", FNM_PATHNAME, FNM_NOMATCH }, + { "b/*", "b/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, + { "b*/*", "a/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, + { "b/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, + { "b*/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, + { "a", "A", FNM_CASEFOLD, 0 }, + { "A", "a", FNM_CASEFOLD, 0 }, + { "[a]", "A", FNM_CASEFOLD, 0 }, + { "[A]", "a", FNM_CASEFOLD, 0 }, + { "a", "b", FNM_CASEFOLD, FNM_NOMATCH }, + { "a", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "a", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*a", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, + { "*a", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, + { "a*b/*", "abbb/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, +}; diff --git a/lib/libc/tests/gen/fpclassify2_test.c b/lib/libc/tests/gen/fpclassify2_test.c new file mode 100644 index 000000000000..45180ac5be42 --- /dev/null +++ b/lib/libc/tests/gen/fpclassify2_test.c @@ -0,0 +1,70 @@ +/*- + * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org> + * 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 AUTHOR 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 AUTHOR 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 <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(test_fpclassify); +ATF_TC_BODY(test_fpclassify, tc) +{ + + ATF_CHECK(fpclassify((float)0) == FP_ZERO); + ATF_CHECK(fpclassify((float)-0.0) == FP_ZERO); + ATF_CHECK(fpclassify((float)1) == FP_NORMAL); + ATF_CHECK(fpclassify((float)1000) == FP_NORMAL); + ATF_CHECK(fpclassify(HUGE_VALF) == FP_INFINITE); + ATF_CHECK(fpclassify((float)HUGE_VAL) == FP_INFINITE); + ATF_CHECK(fpclassify((float)HUGE_VALL) == FP_INFINITE); + ATF_CHECK(fpclassify(NAN) == FP_NAN); + + ATF_CHECK(fpclassify((double)0) == FP_ZERO); + ATF_CHECK(fpclassify((double)-0) == FP_ZERO); + ATF_CHECK(fpclassify((double)1) == FP_NORMAL); + ATF_CHECK(fpclassify((double)1000) == FP_NORMAL); + ATF_CHECK(fpclassify(HUGE_VAL) == FP_INFINITE); + ATF_CHECK(fpclassify((double)HUGE_VALF) == FP_INFINITE); + ATF_CHECK(fpclassify((double)HUGE_VALL) == FP_INFINITE); + ATF_CHECK(fpclassify((double)NAN) == FP_NAN); + + ATF_CHECK(fpclassify((long double)0) == FP_ZERO); + ATF_CHECK(fpclassify((long double)-0.0) == FP_ZERO); + ATF_CHECK(fpclassify((long double)1) == FP_NORMAL); + ATF_CHECK(fpclassify((long double)1000) == FP_NORMAL); + ATF_CHECK(fpclassify(HUGE_VALL) == FP_INFINITE); + ATF_CHECK(fpclassify((long double)HUGE_VALF) == FP_INFINITE); + ATF_CHECK(fpclassify((long double)HUGE_VAL) == FP_INFINITE); + ATF_CHECK(fpclassify((long double)NAN) == FP_NAN); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, test_fpclassify); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fts_blocks_test.c b/lib/libc/tests/gen/fts_blocks_test.c new file mode 100644 index 000000000000..f020dd8dea45 --- /dev/null +++ b/lib/libc/tests/gen/fts_blocks_test.c @@ -0,0 +1,68 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <fcntl.h> +#include <fts.h> + +#include <atf-c.h> + +/* + * Create two directories with three files each in lexicographical order, + * then call FTS with a sort block that sorts in reverse lexicographical + * order. This has the least chance of getting a false positive due to + * differing file system semantics. UFS will return the files in the + * order they were created while ZFS will sort them lexicographically; in + * both cases, the order we expect is the reverse. + */ +ATF_TC(fts_blocks_test); +ATF_TC_HEAD(fts_blocks_test, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test FTS with a block in lieu of a comparison function"); +} +ATF_TC_BODY(fts_blocks_test, tc) +{ + char *args[] = { + "bar", "foo", NULL + }; + char *paths[] = { + "foo", "z", "y", "x", "foo", + "bar", "c", "b", "a", "bar", + NULL + }; + char **expect = paths; + FTS *fts; + FTSENT *ftse; + + ATF_REQUIRE_EQ(0, mkdir("bar", 0755)); + ATF_REQUIRE_EQ(0, close(creat("bar/a", 0644))); + ATF_REQUIRE_EQ(0, close(creat("bar/b", 0644))); + ATF_REQUIRE_EQ(0, close(creat("bar/c", 0644))); + ATF_REQUIRE_EQ(0, mkdir("foo", 0755)); + ATF_REQUIRE_EQ(0, close(creat("foo/x", 0644))); + ATF_REQUIRE_EQ(0, close(creat("foo/y", 0644))); + ATF_REQUIRE_EQ(0, close(creat("foo/z", 0644))); + fts = fts_open_b(args, 0, + ^(const FTSENT * const *a, const FTSENT * const *b) { + return (strcmp((*b)->fts_name, (*a)->fts_name)); + }); + ATF_REQUIRE_MSG(fts != NULL, "fts_open_b(): %m"); + while ((ftse = fts_read(fts)) != NULL && *expect != NULL) { + ATF_CHECK_STREQ(*expect, ftse->fts_name); + expect++; + } + ATF_CHECK_EQ(NULL, ftse); + ATF_CHECK_EQ(NULL, *expect); + ATF_REQUIRE_EQ_MSG(0, fts_close(fts), "fts_close(): %m"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, fts_blocks_test); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fts_misc_test.c b/lib/libc/tests/gen/fts_misc_test.c new file mode 100644 index 000000000000..91640078f63c --- /dev/null +++ b/lib/libc/tests/gen/fts_misc_test.c @@ -0,0 +1,78 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <fcntl.h> +#include <fts.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "fts_test.h" + +ATF_TC(fts_unrdir); +ATF_TC_HEAD(fts_unrdir, tc) +{ + atf_tc_set_md_var(tc, "descr", "unreadable directories"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} +ATF_TC_BODY(fts_unrdir, tc) +{ + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE_EQ(0, mkdir("dir/unr", 0100)); + ATF_REQUIRE_EQ(0, mkdir("dir/unx", 0400)); + fts_test(tc, &(struct fts_testcase){ + (char *[]){ "dir", NULL }, + FTS_PHYSICAL, + (struct fts_expect[]){ + { FTS_D, "dir", "dir" }, + { FTS_D, "unr", "unr" }, + { FTS_DNR, "unr", "unr" }, + { FTS_D, "unx", "unx" }, + { FTS_DP, "unx", "unx" }, + { FTS_DP, "dir", "dir" }, + { 0 } + }, + }); +} + +ATF_TC(fts_unrdir_nochdir); +ATF_TC_HEAD(fts_unrdir_nochdir, tc) +{ + atf_tc_set_md_var(tc, "descr", "unreadable directories (nochdir)"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} +ATF_TC_BODY(fts_unrdir_nochdir, tc) +{ + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE_EQ(0, mkdir("dir/unr", 0100)); + ATF_REQUIRE_EQ(0, mkdir("dir/unx", 0400)); + fts_test(tc, &(struct fts_testcase){ + (char *[]){ "dir", NULL }, + FTS_PHYSICAL | FTS_NOCHDIR, + (struct fts_expect[]){ + { FTS_D, "dir", "dir" }, + { FTS_D, "unr", "dir/unr" }, + { FTS_DNR, "unr", "dir/unr" }, + { FTS_D, "unx", "dir/unx" }, + { FTS_DP, "unx", "dir/unx" }, + { FTS_DP, "dir", "dir" }, + { 0 } + }, + }); +} + +ATF_TP_ADD_TCS(tp) +{ + fts_check_debug(); + ATF_TP_ADD_TC(tp, fts_unrdir); + ATF_TP_ADD_TC(tp, fts_unrdir_nochdir); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fts_options_test.c b/lib/libc/tests/gen/fts_options_test.c new file mode 100644 index 000000000000..fc3015138a49 --- /dev/null +++ b/lib/libc/tests/gen/fts_options_test.c @@ -0,0 +1,394 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <fcntl.h> +#include <fts.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "fts_test.h" + +static char *all_paths[] = { + "dir", + "dirl", + "file", + "filel", + "dead", + "noent", + NULL +}; + +/* + * Prepare the files and directories we will be inspecting. + */ +static void +fts_options_prepare(const struct atf_tc *tc) +{ + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE_EQ(0, close(creat("file", 0644))); + ATF_REQUIRE_EQ(0, close(creat("dir/file", 0644))); + ATF_REQUIRE_EQ(0, symlink("..", "dir/up")); + ATF_REQUIRE_EQ(0, symlink("dir", "dirl")); + ATF_REQUIRE_EQ(0, symlink("file", "filel")); + ATF_REQUIRE_EQ(0, symlink("noent", "dead")); +} + +ATF_TC(fts_options_logical); +ATF_TC_HEAD(fts_options_logical, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_LOGICAL"); +} +ATF_TC_BODY(fts_options_logical, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_LOGICAL, + (struct fts_expect[]){ + { FTS_DL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_F, "file", "dir/file" }, + { FTS_D, "up", "dir/up" }, + { FTS_DL, "dead", "dir/up/dead" }, + { FTS_DC, "dir", "dir/up/dir" }, + { FTS_DC, "dirl", "dir/up/dirl" }, + { FTS_F, "file", "dir/up/file" }, + { FTS_F, "filel", "dir/up/filel" }, + { FTS_DP, "up", "dir/up" }, + { FTS_DP, "dir", "dir" }, + { FTS_D, "dirl", "dirl" }, + { FTS_F, "file", "dirl/file" }, + { FTS_D, "up", "dirl/up" }, + { FTS_DL, "dead", "dirl/up/dead" }, + { FTS_DC, "dir", "dirl/up/dir" }, + { FTS_DC, "dirl", "dirl/up/dirl" }, + { FTS_F, "file", "dirl/up/file" }, + { FTS_F, "filel", "dirl/up/filel" }, + { FTS_DP, "up", "dirl/up" }, + { FTS_DP, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_F, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_logical_nostat); +ATF_TC_HEAD(fts_options_logical_nostat, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_LOGICAL | FTS_NOSTAT"); +} +ATF_TC_BODY(fts_options_logical_nostat, tc) +{ + /* + * While FTS_LOGICAL is not documented as being incompatible with + * FTS_NOSTAT, and FTS does not clear FTS_NOSTAT if FTS_LOGICAL is + * set, FTS_LOGICAL effectively nullifies FTS_NOSTAT by overriding + * the follow check in fts_stat(). In theory, FTS could easily be + * changed to only stat links (to check what they point to) in the + * FTS_LOGICAL | FTS_NOSTAT case, which would produce a different + * result here, so keep the test around in case that ever happens. + */ + atf_tc_expect_fail("FTS_LOGICAL nullifies FTS_NOSTAT"); + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_LOGICAL | FTS_NOSTAT, + (struct fts_expect[]){ + { FTS_DL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_NSOK, "file", "dir/file" }, + { FTS_D, "up", "dir/up" }, + { FTS_DL, "dead", "dir/up/dead" }, + { FTS_DC, "dir", "dir/up/dir" }, + { FTS_DC, "dirl", "dir/up/dirl" }, + { FTS_NSOK, "file", "dir/up/file" }, + { FTS_NSOK, "filel", "dir/up/filel" }, + { FTS_DP, "up", "dir/up" }, + { FTS_DP, "dir", "dir" }, + { FTS_D, "dirl", "dirl" }, + { FTS_NSOK, "file", "dirl/file" }, + { FTS_D, "up", "dirl/up" }, + { FTS_DL, "dead", "dirl/up/dead" }, + { FTS_DC, "dir", "dirl/up/dir" }, + { FTS_DC, "dirl", "dirl/up/dirl" }, + { FTS_NSOK, "file", "dirl/up/file" }, + { FTS_NSOK, "filel", "dirl/up/filel" }, + { FTS_DP, "up", "dirl/up" }, + { FTS_DP, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_F, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_logical_seedot); +ATF_TC_HEAD(fts_options_logical_seedot, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_LOGICAL | FTS_SEEDOT"); +} +ATF_TC_BODY(fts_options_logical_seedot, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_LOGICAL | FTS_SEEDOT, + (struct fts_expect[]){ + { FTS_DL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_DOT, ".", "dir/." }, + { FTS_DOT, "..", "dir/.." }, + { FTS_F, "file", "dir/file" }, + { FTS_D, "up", "dir/up" }, + { FTS_DOT, ".", "dir/up/." }, + { FTS_DOT, "..", "dir/up/.." }, + { FTS_DL, "dead", "dir/up/dead" }, + { FTS_DC, "dir", "dir/up/dir" }, + { FTS_DC, "dirl", "dir/up/dirl" }, + { FTS_F, "file", "dir/up/file" }, + { FTS_F, "filel", "dir/up/filel" }, + { FTS_DP, "up", "dir/up" }, + { FTS_DP, "dir", "dir" }, + { FTS_D, "dirl", "dirl" }, + { FTS_DOT, ".", "dirl/." }, + { FTS_DOT, "..", "dirl/.." }, + { FTS_F, "file", "dirl/file" }, + { FTS_D, "up", "dirl/up" }, + { FTS_DOT, ".", "dirl/up/." }, + { FTS_DOT, "..", "dirl/up/.." }, + { FTS_DL, "dead", "dirl/up/dead" }, + { FTS_DC, "dir", "dirl/up/dir" }, + { FTS_DC, "dirl", "dirl/up/dirl" }, + { FTS_F, "file", "dirl/up/file" }, + { FTS_F, "filel", "dirl/up/filel" }, + { FTS_DP, "up", "dirl/up" }, + { FTS_DP, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_F, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical); +ATF_TC_HEAD(fts_options_physical, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL"); +} +ATF_TC_BODY(fts_options_physical, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL, + (struct fts_expect[]){ + { FTS_SL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dir", "dir" }, + { FTS_SL, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical_nochdir); +ATF_TC_HEAD(fts_options_physical_nochdir, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL | FTS_NOCHDIR"); +} +ATF_TC_BODY(fts_options_physical_nochdir, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL | FTS_NOCHDIR, + (struct fts_expect[]){ + { FTS_SL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_F, "file", "dir/file" }, + { FTS_SL, "up", "dir/up" }, + { FTS_DP, "dir", "dir" }, + { FTS_SL, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical_comfollow); +ATF_TC_HEAD(fts_options_physical_comfollow, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL | FTS_COMFOLLOW"); +} +ATF_TC_BODY(fts_options_physical_comfollow, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL | FTS_COMFOLLOW, + (struct fts_expect[]){ + { FTS_DL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dir", "dir" }, + { FTS_D, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_F, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical_comfollowdir); +ATF_TC_HEAD(fts_options_physical_comfollowdir, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL | FTS_COMFOLLOWDIR"); +} +ATF_TC_BODY(fts_options_physical_comfollowdir, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL | FTS_COMFOLLOWDIR, + (struct fts_expect[]){ + { FTS_DL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dir", "dir" }, + { FTS_D, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical_nostat); +ATF_TC_HEAD(fts_options_physical_nostat, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL | FTS_NOSTAT"); +} +ATF_TC_BODY(fts_options_physical_nostat, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL | FTS_NOSTAT, + (struct fts_expect[]){ + { FTS_SL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_NSOK, "file", "file" }, + { FTS_NSOK, "up", "up" }, + { FTS_DP, "dir", "dir" }, + { FTS_SL, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical_nostat_type); +ATF_TC_HEAD(fts_options_physical_nostat_type, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL | FTS_NOSTAT_TYPE"); +} +ATF_TC_BODY(fts_options_physical_nostat_type, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL | FTS_NOSTAT_TYPE, + (struct fts_expect[]){ + { FTS_SL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dir", "dir" }, + { FTS_SL, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +ATF_TC(fts_options_physical_seedot); +ATF_TC_HEAD(fts_options_physical_seedot, tc) +{ + atf_tc_set_md_var(tc, "descr", "FTS_PHYSICAL | FTS_SEEDOT"); +} +ATF_TC_BODY(fts_options_physical_seedot, tc) +{ + fts_options_prepare(tc); + fts_test(tc, &(struct fts_testcase){ + all_paths, + FTS_PHYSICAL | FTS_SEEDOT, + (struct fts_expect[]){ + { FTS_SL, "dead", "dead" }, + { FTS_D, "dir", "dir" }, + { FTS_DOT, ".", "." }, + { FTS_DOT, "..", ".." }, + { FTS_F, "file", "file" }, + { FTS_SL, "up", "up" }, + { FTS_DP, "dir", "dir" }, + { FTS_SL, "dirl", "dirl" }, + { FTS_F, "file", "file" }, + { FTS_SL, "filel", "filel" }, + { FTS_NS, "noent", "noent" }, + { 0 } + }, + }); +} + +/* + * TODO: Add tests for FTS_XDEV and FTS_WHITEOUT + */ + +ATF_TP_ADD_TCS(tp) +{ + fts_check_debug(); + ATF_TP_ADD_TC(tp, fts_options_logical); + ATF_TP_ADD_TC(tp, fts_options_logical_nostat); + ATF_TP_ADD_TC(tp, fts_options_logical_seedot); + ATF_TP_ADD_TC(tp, fts_options_physical); + ATF_TP_ADD_TC(tp, fts_options_physical_nochdir); + ATF_TP_ADD_TC(tp, fts_options_physical_comfollow); + ATF_TP_ADD_TC(tp, fts_options_physical_comfollowdir); + ATF_TP_ADD_TC(tp, fts_options_physical_nostat); + ATF_TP_ADD_TC(tp, fts_options_physical_nostat_type); + ATF_TP_ADD_TC(tp, fts_options_physical_seedot); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fts_test.h b/lib/libc/tests/gen/fts_test.h new file mode 100644 index 000000000000..b3f15050f265 --- /dev/null +++ b/lib/libc/tests/gen/fts_test.h @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef FTS_TEST_H_INCLUDED +#define FTS_TEST_H_INCLUDED + +struct fts_expect { + int fts_info; + const char *fts_name; + const char *fts_accpath; +}; + +struct fts_testcase { + char **paths; + int fts_options; + struct fts_expect *fts_expect; +}; + +/* shorter name for dead links */ +#define FTS_DL FTS_SLNONE + +/* are we being debugged? */ +static bool fts_test_debug; + +/* + * Set debug flag if appropriate. + */ +static void +fts_check_debug(void) +{ + fts_test_debug = !getenv("__RUNNING_INSIDE_ATF_RUN") && + isatty(STDERR_FILENO); +} + +/* + * Lexical order for reproducability. + */ +static int +fts_lexical_compar(const FTSENT * const *a, const FTSENT * const *b) +{ + return (strcmp((*a)->fts_name, (*b)->fts_name)); +} + +/* + * Run FTS with the specified paths and options and verify that it + * produces the expected result in the correct order. + */ +static void +fts_test(const struct atf_tc *tc, const struct fts_testcase *fts_tc) +{ + FTS *fts; + FTSENT *ftse; + const struct fts_expect *expect = fts_tc->fts_expect; + long level = 0; + + fts = fts_open(fts_tc->paths, fts_tc->fts_options, fts_lexical_compar); + ATF_REQUIRE_MSG(fts != NULL, "fts_open(): %m"); + while ((ftse = fts_read(fts)) != NULL && expect->fts_name != NULL) { + if (expect->fts_info == FTS_DP || expect->fts_info == FTS_DNR) + level--; + if (fts_test_debug) { + fprintf(stderr, "%2ld %2d %s\n", level, + ftse->fts_info, ftse->fts_name); + } + ATF_CHECK_STREQ(expect->fts_name, ftse->fts_name); + ATF_CHECK_STREQ(expect->fts_accpath, ftse->fts_accpath); + ATF_CHECK_INTEQ(expect->fts_info, ftse->fts_info); + ATF_CHECK_INTEQ(level, ftse->fts_level); + if (expect->fts_info == FTS_D) + level++; + expect++; + } + ATF_CHECK_EQ(NULL, ftse); + ATF_CHECK_EQ(NULL, expect->fts_name); + ATF_REQUIRE_EQ_MSG(0, fts_close(fts), "fts_close(): %m"); +} + +#endif /* FTS_TEST_H_INCLUDED */ diff --git a/lib/libc/tests/gen/ftw_test.c b/lib/libc/tests/gen/ftw_test.c new file mode 100644 index 000000000000..3d2cf3446dee --- /dev/null +++ b/lib/libc/tests/gen/ftw_test.c @@ -0,0 +1,124 @@ +/*- + * Copyright (c) 2012 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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. + */ + +/* + * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008. + */ + +#include <sys/wait.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <ftw.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <unistd.h> + +#include <atf-c.h> + +extern char **environ; + +static char template[] = "testftw.XXXXXXXXXX"; +static char dir[PATH_MAX]; +static int ftwflags; + +static int +cb(const char *path, const struct stat *st, int type, struct FTW *f) +{ + + switch (type) { + case FTW_D: + if ((ftwflags & FTW_DEPTH) == 0) + return (0); + break; + case FTW_DP: + if ((ftwflags & FTW_DEPTH) != 0) + return (0); + break; + case FTW_SL: + if ((ftwflags & FTW_PHYS) != 0) + return (0); + break; + } + ATF_CHECK_MSG(false, + "unexpected path=%s type=%d f.level=%d\n", + path, type, f->level); + return (0); +} + +ATF_TC_WITHOUT_HEAD(ftw_test); +ATF_TC_BODY(ftw_test, tc) +{ + int fd; + + ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed"); + + /* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */ + ATF_REQUIRE_MSG(realpath(template, dir) != NULL, + "realpath failed; errno=%d", errno); + + fd = open(dir, O_DIRECTORY|O_RDONLY); + ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno); + + ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0, + "mkdirat failed; errno=%d", errno); + + ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0, + "symlinkat failed; errno=%d", errno); + + printf("ftwflags=FTW_PHYS\n"); + ftwflags = FTW_PHYS; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw FTW_PHYS failed; errno=%d", errno); + + printf("ftwflags=FTW_PHYS|FTW_DEPTH\n"); + ftwflags = FTW_PHYS|FTW_DEPTH; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno); + + printf("ftwflags=0\n"); + ftwflags = 0; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw 0 failed; errno=%d", errno); + + printf("ftwflags=FTW_DEPTH\n"); + ftwflags = FTW_DEPTH; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw FTW_DEPTH failed; errno=%d", errno); + + close(fd); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, ftw_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/getentropy_test.c b/lib/libc/tests/gen/getentropy_test.c new file mode 100644 index 000000000000..6ac9d5678ea6 --- /dev/null +++ b/lib/libc/tests/gen/getentropy_test.c @@ -0,0 +1,85 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org> + * 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 AUTHOR 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 AUTHOR 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/param.h> +#include <errno.h> +#include <limits.h> +#include <signal.h> +#include <unistd.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(getentropy_count); +ATF_TC_BODY(getentropy_count, tc) +{ + char buf[2]; + int ret; + + /* getentropy(2) does not modify buf past the requested length */ + buf[1] = 0x7C; + ret = getentropy(buf, 1); + ATF_REQUIRE_EQ(ret, 0); + ATF_REQUIRE_EQ(buf[1], 0x7C); +} + +ATF_TC_WITHOUT_HEAD(getentropy_fault); +ATF_TC_BODY(getentropy_fault, tc) +{ + int ret; + + ret = getentropy(NULL, 1); + ATF_REQUIRE_EQ(ret, -1); + ATF_REQUIRE_EQ(errno, EFAULT); +} + +ATF_TC_WITHOUT_HEAD(getentropy_sizes); +ATF_TC_BODY(getentropy_sizes, tc) +{ + char buf[512]; + + ATF_REQUIRE_EQ(getentropy(buf, sizeof(buf)), -1); + ATF_REQUIRE_EQ(errno, EINVAL); + ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX + 1), -1); + ATF_REQUIRE_EQ(errno, EINVAL); + + /* Smaller sizes always succeed: */ + ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX), 0); + ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX / 2), 0); + ATF_REQUIRE_EQ(getentropy(buf, 0), 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + signal(SIGSYS, SIG_IGN); + + ATF_TP_ADD_TC(tp, getentropy_count); + ATF_TP_ADD_TC(tp, getentropy_fault); + ATF_TP_ADD_TC(tp, getentropy_sizes); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/getmntinfo_test.c b/lib/libc/tests/gen/getmntinfo_test.c new file mode 100644 index 000000000000..06e1091d8a15 --- /dev/null +++ b/lib/libc/tests/gen/getmntinfo_test.c @@ -0,0 +1,83 @@ +/*- + * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org> + * 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 AUTHOR 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 AUTHOR 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. + */ + +/* + * Limited test program for getmntinfo(3), a non-standard BSDism. + */ + +#include <sys/param.h> +#include <sys/mount.h> +#include <sys/ucred.h> + +#include <errno.h> + +#include <atf-c.h> + +static void +check_mntinfo(struct statfs *mntinfo, int n) +{ + int i; + + for (i = 0; i < n; i++) { + ATF_REQUIRE_MSG(mntinfo[i].f_version == STATFS_VERSION, "%ju", + (uintmax_t)mntinfo[i].f_version); + ATF_REQUIRE(mntinfo[i].f_namemax <= sizeof(mntinfo[0].f_mntonname)); + } +} + +ATF_TC_WITHOUT_HEAD(getmntinfo_test); +ATF_TC_BODY(getmntinfo_test, tc) +{ + int nmnts; + struct statfs *mntinfo; + + /* Test bogus mode */ + nmnts = getmntinfo(&mntinfo, 199); + ATF_REQUIRE_MSG(nmnts == 0 && errno == EINVAL, + "getmntinfo() succeeded; errno=%d", errno); + + /* Valid modes */ + nmnts = getmntinfo(&mntinfo, MNT_NOWAIT); + ATF_REQUIRE_MSG(nmnts != 0, "getmntinfo(MNT_NOWAIT) failed; errno=%d", + errno); + + check_mntinfo(mntinfo, nmnts); + memset(mntinfo, 0xdf, sizeof(*mntinfo) * nmnts); + + nmnts = getmntinfo(&mntinfo, MNT_WAIT); + ATF_REQUIRE_MSG(nmnts != 0, "getmntinfo(MNT_WAIT) failed; errno=%d", + errno); + + check_mntinfo(mntinfo, nmnts); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, getmntinfo_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/glob2_test.c b/lib/libc/tests/gen/glob2_test.c new file mode 100644 index 000000000000..ff1b36b830b8 --- /dev/null +++ b/lib/libc/tests/gen/glob2_test.c @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2017 Dell EMC Isilon + * 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 AUTHOR 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 AUTHOR 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/param.h> +#include <sys/stat.h> + +#include <errno.h> +#include <fcntl.h> +#include <glob.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include <atf-c.h> + +/* + * Derived from Russ Cox' pathological case test program used for the + * https://research.swtch.com/glob article. + */ +ATF_TC_WITHOUT_HEAD(glob_pathological_test); +ATF_TC_BODY(glob_pathological_test, tc) +{ + struct timespec t, t2; + glob_t g; + const char *longname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + char pattern[1000], *p; + double dt; + unsigned i, j, k, mul; + int fd, rc; + + fd = open(longname, O_CREAT | O_RDWR, 0666); + ATF_REQUIRE(fd >= 0); + + /* + * Test up to 100 a* groups. Exponential implementations typically go + * bang at i=7 or 8. + */ + for (i = 0; i < 100; i++) { + /* + * Create a*...b pattern with i 'a*' groups. + */ + p = pattern; + for (k = 0; k < i; k++) { + *p++ = 'a'; + *p++ = '*'; + } + *p++ = 'b'; + *p = '\0'; + + clock_gettime(CLOCK_REALTIME, &t); + for (j = 0; j < mul; j++) { + memset(&g, 0, sizeof g); + rc = glob(pattern, 0, 0, &g); + if (rc == GLOB_NOSPACE || rc == GLOB_ABORTED) { + ATF_REQUIRE_MSG(rc == GLOB_NOMATCH, + "an unexpected error occurred: " + "rc=%d errno=%d", rc, errno); + /* NORETURN */ + } + + ATF_CHECK_MSG(rc == GLOB_NOMATCH, + "A bogus match occurred: '%s' ~ '%s'", pattern, + g.gl_pathv[0]); + globfree(&g); + } + clock_gettime(CLOCK_REALTIME, &t2); + + t2.tv_sec -= t.tv_sec; + t2.tv_nsec -= t.tv_nsec; + dt = t2.tv_sec + (double)t2.tv_nsec/1e9; + dt /= mul; + + ATF_CHECK_MSG(dt < 1, "glob(3) took far too long: %d %.9f", i, + dt); + + if (dt >= 0.0001) + mul = 1; + } +} + +ATF_TC(glob_period); +ATF_TC_HEAD(glob_period, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test behaviour when matching files that start with a period" + "(documented in the glob(3) CAVEATS section)."); +} +ATF_TC_BODY(glob_period, tc) +{ + int i; + glob_t g; + + atf_utils_create_file(".test", ""); + glob(".", 0, NULL, &g); + ATF_REQUIRE_MSG(g.gl_matchc == 1, + "glob(3) shouldn't match files starting with a period when using '.'"); + for (i = 0; i < g.gl_matchc; i++) + printf("%s\n", g.gl_pathv[i]); + glob(".*", 0, NULL, &g); + ATF_REQUIRE_MSG(g.gl_matchc == 3 && strcmp(g.gl_pathv[2], ".test") == 0, + "glob(3) should match files starting with a period when using '.*'"); +} + +static bool glob_callback_invoked; + +static int +errfunc(const char *path, int err) +{ + ATF_CHECK_STREQ(path, "test/"); + ATF_CHECK(err == EACCES); + glob_callback_invoked = true; + /* Suppress EACCES errors. */ + return (0); +} + +ATF_TC(glob_callback); +ATF_TC_HEAD(glob_callback, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test ability of callback function to suppress errors"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} +ATF_TC_BODY(glob_callback, tc) +{ + glob_t g; + int rv; + + ATF_REQUIRE_EQ(0, mkdir("test", 0755)); + ATF_REQUIRE_EQ(0, symlink("foo", "test/foo")); + ATF_REQUIRE_EQ(0, chmod("test", 0)); + + glob_callback_invoked = false; + rv = glob("test/*", 0, errfunc, &g); + ATF_CHECK_MSG(glob_callback_invoked, + "glob(3) failed to invoke callback function"); + ATF_CHECK_EQ_MSG(GLOB_NOMATCH, rv, + "callback function failed to suppress EACCES"); + globfree(&g); + + /* GLOB_ERR should ignore the suppressed error. */ + glob_callback_invoked = false; + rv = glob("test/*", GLOB_ERR, errfunc, &g); + ATF_CHECK_MSG(glob_callback_invoked, + "glob(3) failed to invoke callback function"); + ATF_CHECK_EQ_MSG(GLOB_ABORTED, rv, + "GLOB_ERR didn't override callback function"); + globfree(&g); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, glob_pathological_test); + ATF_TP_ADD_TC(tp, glob_period); + ATF_TP_ADD_TC(tp, glob_callback); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/glob_blocks_test.c b/lib/libc/tests/gen/glob_blocks_test.c new file mode 100644 index 000000000000..629b90bee762 --- /dev/null +++ b/lib/libc/tests/gen/glob_blocks_test.c @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <errno.h> +#include <glob.h> +#include <stdbool.h> + +#include <atf-c.h> + +ATF_TC(glob_b_callback); +ATF_TC_HEAD(glob_b_callback, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test ability of callback block to suppress errors"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} +ATF_TC_BODY(glob_b_callback, tc) +{ + static bool glob_callback_invoked; + static int (^errblk)(const char *, int) = + ^(const char *path, int err) { + ATF_CHECK_STREQ(path, "test/"); + ATF_CHECK(err == EACCES); + glob_callback_invoked = true; + /* Suppress EACCES errors. */ + return (0); + }; + glob_t g; + int rv; + + ATF_REQUIRE_EQ(0, mkdir("test", 0755)); + ATF_REQUIRE_EQ(0, symlink("foo", "test/foo")); + ATF_REQUIRE_EQ(0, chmod("test", 0)); + + glob_callback_invoked = false; + rv = glob_b("test/*", 0, errblk, &g); + ATF_CHECK_MSG(glob_callback_invoked, + "glob(3) failed to invoke callback block"); + ATF_CHECK_EQ_MSG(GLOB_NOMATCH, rv, + "callback function failed to suppress EACCES"); + globfree(&g); + + /* GLOB_ERR should ignore the suppressed error. */ + glob_callback_invoked = false; + rv = glob_b("test/*", GLOB_ERR, errblk, &g); + ATF_CHECK_MSG(glob_callback_invoked, + "glob(3) failed to invoke callback block"); + ATF_CHECK_EQ_MSG(GLOB_ABORTED, rv, + "GLOB_ERR didn't override callback block"); + globfree(&g); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, glob_b_callback); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/makecontext_test.c b/lib/libc/tests/gen/makecontext_test.c new file mode 100644 index 000000000000..23e3cf85f677 --- /dev/null +++ b/lib/libc/tests/gen/makecontext_test.c @@ -0,0 +1,185 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2018 John H. Baldwin <jhb@FreeBSD.org> + * + * 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 AUTHOR 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 AUTHOR 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 <atf-c.h> +#include <ucontext.h> + +static char uc_stack[16 * 1024]; + +static void +check_1(int arg1) +{ + + ATF_REQUIRE_EQ(arg1, 1); +} + +ATF_TC_WITHOUT_HEAD(makecontext_arg1); +ATF_TC_BODY(makecontext_arg1, tc) +{ + ucontext_t ctx[2]; + + ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0); + ctx[1].uc_stack.ss_sp = uc_stack; + ctx[1].uc_stack.ss_size = sizeof(uc_stack); + ctx[1].uc_link = &ctx[0]; + makecontext(&ctx[1], (void (*)(void))check_1, 1, 1); + + ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0); +} + +static void +check_2(int arg1, int arg2) +{ + + ATF_REQUIRE_EQ(arg1, 1); + ATF_REQUIRE_EQ(arg2, 2); +} + +ATF_TC_WITHOUT_HEAD(makecontext_arg2); +ATF_TC_BODY(makecontext_arg2, tc) +{ + ucontext_t ctx[2]; + + ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0); + ctx[1].uc_stack.ss_sp = uc_stack; + ctx[1].uc_stack.ss_size = sizeof(uc_stack); + ctx[1].uc_link = &ctx[0]; + makecontext(&ctx[1], (void (*)(void))check_2, 2, 1, 2); + + ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0); +} + +static void +check_3(int arg1, int arg2, int arg3) +{ + + ATF_REQUIRE_EQ(arg1, 1); + ATF_REQUIRE_EQ(arg2, 2); + ATF_REQUIRE_EQ(arg3, 3); +} + +ATF_TC_WITHOUT_HEAD(makecontext_arg3); +ATF_TC_BODY(makecontext_arg3, tc) +{ + ucontext_t ctx[2]; + + ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0); + ctx[1].uc_stack.ss_sp = uc_stack; + ctx[1].uc_stack.ss_size = sizeof(uc_stack); + ctx[1].uc_link = &ctx[0]; + makecontext(&ctx[1], (void (*)(void))check_3, 3, 1, 2, 3); + + ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0); +} + +static void +check_4(int arg1, int arg2, int arg3, int arg4) +{ + + ATF_REQUIRE_EQ(arg1, 1); + ATF_REQUIRE_EQ(arg2, 2); + ATF_REQUIRE_EQ(arg3, 3); + ATF_REQUIRE_EQ(arg4, 4); +} + +ATF_TC_WITHOUT_HEAD(makecontext_arg4); +ATF_TC_BODY(makecontext_arg4, tc) +{ + ucontext_t ctx[2]; + + ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0); + ctx[1].uc_stack.ss_sp = uc_stack; + ctx[1].uc_stack.ss_size = sizeof(uc_stack); + ctx[1].uc_link = &ctx[0]; + makecontext(&ctx[1], (void (*)(void))check_4, 4, 1, 2, 3, 4); + + ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0); +} + +static void +check_5(int arg1, int arg2, int arg3, int arg4, int arg5) +{ + + ATF_REQUIRE_EQ(arg1, 1); + ATF_REQUIRE_EQ(arg2, 2); + ATF_REQUIRE_EQ(arg3, 3); + ATF_REQUIRE_EQ(arg4, 4); + ATF_REQUIRE_EQ(arg5, 5); +} + +ATF_TC_WITHOUT_HEAD(makecontext_arg5); +ATF_TC_BODY(makecontext_arg5, tc) +{ + ucontext_t ctx[2]; + + ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0); + ctx[1].uc_stack.ss_sp = uc_stack; + ctx[1].uc_stack.ss_size = sizeof(uc_stack); + ctx[1].uc_link = &ctx[0]; + makecontext(&ctx[1], (void (*)(void))check_5, 5, 1, 2, 3, 4, 5); + + ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0); +} + +static void +check_6(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) +{ + + ATF_REQUIRE_EQ(arg1, 1); + ATF_REQUIRE_EQ(arg2, 2); + ATF_REQUIRE_EQ(arg3, 3); + ATF_REQUIRE_EQ(arg4, 4); + ATF_REQUIRE_EQ(arg5, 5); + ATF_REQUIRE_EQ(arg6, 6); +} + +ATF_TC_WITHOUT_HEAD(makecontext_arg6); +ATF_TC_BODY(makecontext_arg6, tc) +{ + ucontext_t ctx[2]; + + ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0); + ctx[1].uc_stack.ss_sp = uc_stack; + ctx[1].uc_stack.ss_size = sizeof(uc_stack); + ctx[1].uc_link = &ctx[0]; + makecontext(&ctx[1], (void (*)(void))check_6, 6, 1, 2, 3, 4, 5, 6); + + ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, makecontext_arg1); + ATF_TP_ADD_TC(tp, makecontext_arg2); + ATF_TP_ADD_TC(tp, makecontext_arg3); + ATF_TP_ADD_TC(tp, makecontext_arg4); + ATF_TP_ADD_TC(tp, makecontext_arg5); + ATF_TP_ADD_TC(tp, makecontext_arg6); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/opendir_test.c b/lib/libc/tests/gen/opendir_test.c new file mode 100644 index 000000000000..b7481255654f --- /dev/null +++ b/lib/libc/tests/gen/opendir_test.c @@ -0,0 +1,145 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +/* + * Create a directory with a single subdirectory. + */ +static void +opendir_prepare(const struct atf_tc *tc) +{ + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE_EQ(0, mkdir("dir/subdir", 0755)); +} + +/* + * Assuming dirp represents the directory created by opendir_prepare(), + * verify that readdir() returns what we expected to see there. + */ +static void +opendir_check(const struct atf_tc *tc, DIR *dirp) +{ + struct dirent *ent; + + ATF_REQUIRE((ent = readdir(dirp)) != NULL); + ATF_CHECK_EQ(1, ent->d_namlen); + ATF_CHECK_STREQ(".", ent->d_name); + ATF_CHECK_EQ(DT_DIR, ent->d_type); + ATF_REQUIRE((ent = readdir(dirp)) != NULL); + ATF_CHECK_EQ(2, ent->d_namlen); + ATF_CHECK_STREQ("..", ent->d_name); + ATF_CHECK_EQ(DT_DIR, ent->d_type); + ATF_REQUIRE((ent = readdir(dirp)) != NULL); + ATF_CHECK_EQ(sizeof("subdir") - 1, ent->d_namlen); + ATF_CHECK_STREQ("subdir", ent->d_name); + ATF_CHECK_EQ(DT_DIR, ent->d_type); + ATF_CHECK(readdir(dirp) == NULL); + ATF_CHECK(readdir(dirp) == NULL); +} + +ATF_TC(opendir_ok); +ATF_TC_HEAD(opendir_ok, tc) +{ + atf_tc_set_md_var(tc, "descr", "Open a directory."); +} +ATF_TC_BODY(opendir_ok, tc) +{ + DIR *dirp; + + opendir_prepare(tc); + ATF_REQUIRE((dirp = opendir("dir")) != NULL); + opendir_check(tc, dirp); + ATF_CHECK_EQ(0, closedir(dirp)); +} + +ATF_TC(opendir_fifo); +ATF_TC_HEAD(opendir_fifo, tc) +{ + atf_tc_set_md_var(tc, "descr", "Do not hang if given a named pipe."); +} +ATF_TC_BODY(opendir_fifo, tc) +{ + DIR *dirp; + int fd; + + ATF_REQUIRE((fd = mkfifo("fifo", 0644)) >= 0); + ATF_REQUIRE_EQ(0, close(fd)); + ATF_REQUIRE((dirp = opendir("fifo")) == NULL); + ATF_CHECK_EQ(ENOTDIR, errno); +} + +ATF_TC(fdopendir_ok); +ATF_TC_HEAD(fdopendir_ok, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Open a directory from a directory descriptor."); +} +ATF_TC_BODY(fdopendir_ok, tc) +{ + DIR *dirp; + int dd; + + opendir_prepare(tc); + ATF_REQUIRE((dd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0); + ATF_REQUIRE((dirp = fdopendir(dd)) != NULL); + opendir_check(tc, dirp); + ATF_CHECK_EQ(dd, fdclosedir(dirp)); + ATF_CHECK_EQ(0, close(dd)); +} + +ATF_TC(fdopendir_ebadf); +ATF_TC_HEAD(fdopendir_ebadf, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Open a directory from an invalid descriptor."); +} +ATF_TC_BODY(fdopendir_ebadf, tc) +{ + DIR *dirp; + int dd; + + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE((dd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0); + ATF_CHECK_EQ(0, close(dd)); + ATF_REQUIRE((dirp = fdopendir(dd)) == NULL); + ATF_CHECK_EQ(EBADF, errno); +} + +ATF_TC(fdopendir_enotdir); +ATF_TC_HEAD(fdopendir_enotdir, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Open a directory from a non-directory descriptor."); +} +ATF_TC_BODY(fdopendir_enotdir, tc) +{ + DIR *dirp; + int fd; + + ATF_REQUIRE((fd = open("file", O_CREAT | O_RDWR, 0644)) >= 0); + ATF_REQUIRE((dirp = fdopendir(fd)) == NULL); + ATF_CHECK_EQ(ENOTDIR, errno); + ATF_CHECK_EQ(0, close(fd)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, opendir_ok); + ATF_TP_ADD_TC(tp, fdopendir_ok); + ATF_TP_ADD_TC(tp, fdopendir_ebadf); + ATF_TP_ADD_TC(tp, fdopendir_enotdir); + ATF_TP_ADD_TC(tp, opendir_fifo); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/popen_test.c b/lib/libc/tests/gen/popen_test.c new file mode 100644 index 000000000000..43eadd380f39 --- /dev/null +++ b/lib/libc/tests/gen/popen_test.c @@ -0,0 +1,248 @@ +/*- + * Copyright (c) 2013 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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. + */ + +/* + * Limited test program for popen() as specified by IEEE Std. 1003.1-2008, + * with BSD extensions. + */ + +#include <sys/param.h> +#include <sys/wait.h> +#include <errno.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +static volatile sig_atomic_t got_sigpipe; + +static void +sigpipe_handler(int sig __unused) +{ + got_sigpipe = 1; +} + +static void +check_cloexec(FILE *fp, const char *mode) +{ + int exp_flags, flags; + + flags = fcntl(fileno(fp), F_GETFD); + ATF_CHECK_MSG(flags != -1, "fcntl(F_GETFD) failed; errno=%d", errno); + if (flags == -1) + return; + if (strchr(mode, 'e') != NULL) + exp_flags = FD_CLOEXEC; + else + exp_flags = 0; + ATF_CHECK_MSG((flags & FD_CLOEXEC) == exp_flags, + "bad cloexec flag; %d != %d", flags, exp_flags); +} + +ATF_TC_WITHOUT_HEAD(popen_all_modes_test); +ATF_TC_BODY(popen_all_modes_test, tc) +{ + FILE *fp; + int i, status; + const char *mode; + const char *allmodes[] = { "r", "w", "r+", "re", "we", "r+e", "re+" }; + + for (i = 0; i < nitems(allmodes); i++) { + mode = allmodes[i]; + fp = popen("exit 7", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 7, + "bad exit status (no I/O)"); + } +} + +ATF_TC_WITHOUT_HEAD(popen_rmodes_test); +ATF_TC_BODY(popen_rmodes_test, tc) +{ + FILE *fp; + const char *rmodes[] = { "r", "r+", "re", "r+e", "re+" }; + const char *mode; + char buf[80]; + int i, status; + + for (i = 0; i < nitems(rmodes); i++) { + mode = rmodes[i]; + fp = popen("exit 9", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + bool input_error_1 = !(fgetc(fp) != EOF || !feof(fp) || !ferror(fp)); + ATF_CHECK_MSG(!input_error_1, "input error 1"); + if (input_error_1) + continue; + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 9, + "bad exit status (input)"); + } + + for (i = 0; i < nitems(rmodes); i++) { + char *sres; + mode = rmodes[i]; + fp = popen("echo hi there", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + ATF_CHECK_MSG((sres = fgets(buf, sizeof(buf), fp)) != NULL, + "Input error 2"); + if (sres != NULL) + ATF_CHECK_MSG(strcmp(buf, "hi there\n") == 0, + "Bad input 1"); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "Bad exit status (input)"); + } +} + +ATF_TC_WITHOUT_HEAD(popen_wmodes_test); +ATF_TC_BODY(popen_wmodes_test, tc) +{ + FILE *fp, *fp2; + const char *wmodes[] = { "w", "r+", "we", "r+e", "re+" }; + const char *mode; + struct sigaction act, oact; + int i, j, status; + + for (i = 0; i < nitems(wmodes); i++) { + mode = wmodes[i]; + fp = popen("read x && [ \"$x\" = abcd ]", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + ATF_CHECK_MSG(fputs("abcd\n", fp) != EOF, + "Output error 1"); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "Bad exit status (output)"); + } + + act.sa_handler = sigpipe_handler; + act.sa_flags = SA_RESTART; + sigemptyset(&act.sa_mask); + ATF_CHECK_MSG(sigaction(SIGPIPE, &act, &oact) != -1, + "sigaction() failed"); + for (i = 0; i < nitems(wmodes); i++) { + mode = wmodes[i]; + fp = popen("exit 88", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + got_sigpipe = 0; + while (fputs("abcd\n", fp) != EOF) + ; + ATF_CHECK_MSG(ferror(fp) && errno == EPIPE, "Expected EPIPE"); + ATF_CHECK_MSG(got_sigpipe, "Expected SIGPIPE"); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 88, + "Bad exit status (EPIPE)"); + } + ATF_CHECK_MSG(sigaction(SIGPIPE, &oact, NULL) != -1, + "sigaction() failed"); + + for (i = 0; i < nitems(wmodes); i++) { + for (j = 0; j < nitems(wmodes); j++) { + mode = wmodes[i]; + fp = popen("read x", mode); + ATF_CHECK_MSG(fp != NULL, + "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + mode = wmodes[j]; + fp2 = popen("read x", mode); + ATF_CHECK_MSG(fp2 != NULL, + "popen(, \"%s\") failed", mode); + if (fp2 == NULL) { + pclose(fp); + continue; + } + /* If fp2 inherits fp's pipe, we will deadlock here. */ + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 1, + "bad exit status (2 pipes)"); + status = pclose(fp2); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 1, + "bad exit status (2 pipes)"); + } + } +} + +ATF_TC_WITHOUT_HEAD(popen_rwmodes_test); +ATF_TC_BODY(popen_rwmodes_test, tc) +{ + const char *rwmodes[] = { "r+", "r+e", "re+" }; + FILE *fp; + const char *mode; + char *sres; + char buf[80]; + int i, ires, status; + + for (i = 0; i < nitems(rwmodes); i++) { + mode = rwmodes[i]; + fp = popen("read x && printf '%s\\n' \"Q${x#a}\"", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + ATF_CHECK_MSG((ires = fputs("abcd\n", fp)) != EOF, + "Output error 2"); + if (ires != EOF) { + sres = fgets(buf, sizeof(buf), fp); + ATF_CHECK_MSG(sres != NULL, "Input error 3"); + if (sres != NULL) + ATF_CHECK_MSG(strcmp(buf, "Qbcd\n") == 0, + "Bad input 2"); + } + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "bad exit status (I/O)"); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, popen_all_modes_test); + ATF_TP_ADD_TC(tp, popen_rmodes_test); + ATF_TP_ADD_TC(tp, popen_wmodes_test); + ATF_TP_ADD_TC(tp, popen_rwmodes_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/posix_spawn/Makefile b/lib/libc/tests/gen/posix_spawn/Makefile new file mode 100644 index 000000000000..df428876708b --- /dev/null +++ b/lib/libc/tests/gen/posix_spawn/Makefile @@ -0,0 +1,32 @@ +.include <bsd.own.mk> + +BINDIR= ${TESTSDIR} + +NETBSD_ATF_TESTS_C= fileactions_test +NETBSD_ATF_TESTS_C+= spawn_test +NETBSD_ATF_TESTS_C+= spawnattr_test + +PROGS= h_fileactions +PROGS+= h_spawn +PROGS+= h_spawnattr + +SCRIPTS= h_nonexec +SCRIPTS+= h_zero + +CLEANFILES+= h_nonexec + +.include "../../Makefile.netbsd-tests" + +# The dd status=none option is non-standard. Only use it when this test succeeds +# rather than require dd to be a bootstrap tool. +DD_NOSTATUS!=(dd status=none count=0 2> /dev/null && echo status=none) || true +DD=dd ${DD_NOSTATUS} +h_zero: + ${DD} if=/dev/zero of=h_zero bs=1k count=2 + chmod a+x h_zero + +CLEANFILES+= h_zero + +WARNS?=3 + +.include <bsd.test.mk> diff --git a/lib/libc/tests/gen/posix_spawn/Makefile.depend b/lib/libc/tests/gen/posix_spawn/Makefile.depend new file mode 100644 index 000000000000..e89a5c52c82a --- /dev/null +++ b/lib/libc/tests/gen/posix_spawn/Makefile.depend @@ -0,0 +1,18 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/atf/libatf-c \ + lib/libc \ + lib/libcompiler_rt \ + lib/libnetbsd \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/lib/libc/tests/gen/posix_spawn_test.c b/lib/libc/tests/gen/posix_spawn_test.c new file mode 100644 index 000000000000..22133cf1d59a --- /dev/null +++ b/lib/libc/tests/gen/posix_spawn_test.c @@ -0,0 +1,186 @@ +/*- + * Copyright (c) 2011 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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. + */ + +/* + * Test program for posix_spawn() and posix_spawnp() as specified by + * IEEE Std. 1003.1-2008. + */ + +#include <sys/param.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <spawn.h> + +#include <atf-c.h> + +static const char true_script[] = + "#!/usr/bin/env\n" + "/usr/bin/true\n"; + +char *myenv[2] = { "answer=42", NULL }; + +ATF_TC_WITHOUT_HEAD(posix_spawn_simple_test); +ATF_TC_BODY(posix_spawn_simple_test, tc) +{ + char *myargs[4]; + int error, status; + pid_t pid, waitres; + + /* Make sure we have no child processes. */ + while (waitpid(-1, NULL, 0) != -1) + ; + ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno); + + /* Simple test. */ + myargs[0] = "sh"; + myargs[1] = "-c"; + myargs[2] = "exit $answer"; + myargs[3] = NULL; + error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv); + ATF_REQUIRE(error == 0); + waitres = waitpid(pid, &status, 0); + ATF_REQUIRE(waitres == pid); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42); +} + +ATF_TC_WITHOUT_HEAD(posix_spawn_no_such_command_negative_test); +ATF_TC_BODY(posix_spawn_no_such_command_negative_test, tc) +{ + char *myargs[4]; + int error, status; + pid_t pid, waitres; + + /* + * If the executable does not exist, the function shall either fail + * and not create a child process or succeed and create a child + * process that exits with status 127. + */ + myargs[0] = "/var/empty/nonexistent"; + myargs[1] = NULL; + error = posix_spawn(&pid, myargs[0], NULL, NULL, myargs, myenv); + if (error == 0) { + waitres = waitpid(pid, &status, 0); + ATF_REQUIRE(waitres == pid); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127); + } else { + ATF_REQUIRE(error == ENOENT); + waitres = waitpid(-1, NULL, 0); + ATF_REQUIRE(waitres == -1 && errno == ECHILD); + } +} + +ATF_TC_WITHOUT_HEAD(posix_spawnp_enoexec_fallback); +ATF_TC_BODY(posix_spawnp_enoexec_fallback, tc) +{ + char buf[FILENAME_MAX]; + char *myargs[2]; + int error, status; + pid_t pid, waitres; + + snprintf(buf, sizeof(buf), "%s/spawnp_enoexec.sh", + atf_tc_get_config_var(tc, "srcdir")); + myargs[0] = buf; + myargs[1] = NULL; + error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv); + ATF_REQUIRE(error == 0); + waitres = waitpid(pid, &status, 0); + ATF_REQUIRE(waitres == pid); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42); +} + +ATF_TC_WITHOUT_HEAD(posix_spawnp_enoexec_fallback_null_argv0); +ATF_TC_BODY(posix_spawnp_enoexec_fallback_null_argv0, tc) +{ + char buf[FILENAME_MAX]; + char *myargs[1]; + int error; + pid_t pid; + + snprintf(buf, sizeof(buf), "%s/spawnp_enoexec.sh", + atf_tc_get_config_var(tc, "srcdir")); + myargs[0] = NULL; + error = posix_spawnp(&pid, buf, NULL, NULL, myargs, myenv); + ATF_REQUIRE(error == EINVAL); +} + +ATF_TC(posix_spawnp_eacces); +ATF_TC_HEAD(posix_spawnp_eacces, tc) +{ + atf_tc_set_md_var(tc, "descr", "Verify EACCES behavior in posix_spawnp"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} +ATF_TC_BODY(posix_spawnp_eacces, tc) +{ + const struct spawnp_eacces_tc { + const char *pathvar; + int error_expected; + } spawnp_eacces_tests[] = { + { ".", EACCES }, /* File exists, but not +x */ + { "unsearchable", ENOENT }, /* File exists, dir not +x */ + }; + char *myargs[2] = { "eacces", NULL }; + int error; + + error = mkdir("unsearchable", 0755); + ATF_REQUIRE(error == 0); + error = symlink("/usr/bin/true", "unsearchable/eacces"); + ATF_REQUIRE(error == 0); + + (void)chmod("unsearchable", 0444); + + /* this will create a non-executable file */ + atf_utils_create_file("eacces", true_script); + + for (size_t i = 0; i < nitems(spawnp_eacces_tests); i++) { + const struct spawnp_eacces_tc *tc = &spawnp_eacces_tests[i]; + pid_t pid; + + error = setenv("PATH", tc->pathvar, 1); + ATF_REQUIRE_EQ(0, error); + + error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, + myenv); + ATF_CHECK_INTEQ_MSG(tc->error_expected, error, + "path '%s'", tc->pathvar); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, posix_spawn_simple_test); + ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test); + ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback); + ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback_null_argv0); + ATF_TP_ADD_TC(tp, posix_spawnp_eacces); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/realpath2_test.c b/lib/libc/tests/gen/realpath2_test.c new file mode 100644 index 000000000000..431df8721ae0 --- /dev/null +++ b/lib/libc/tests/gen/realpath2_test.c @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2017 Jan Kokemüller + * All rights reserved. + * Copyright (c) 2025 Klara, Inc. + * + * 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 AUTHOR 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 AUTHOR 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/param.h> +#include <sys/stat.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +ATF_TC(realpath_null); +ATF_TC_HEAD(realpath_null, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test null input"); +} +ATF_TC_BODY(realpath_null, tc) +{ + ATF_REQUIRE_ERRNO(EINVAL, realpath(NULL, NULL) == NULL); +} + +ATF_TC(realpath_empty); +ATF_TC_HEAD(realpath_empty, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test empty input"); +} +ATF_TC_BODY(realpath_empty, tc) +{ + char resb[PATH_MAX] = ""; + + ATF_REQUIRE_EQ(0, mkdir("foo", 0755)); + ATF_REQUIRE_EQ(0, chdir("foo")); + ATF_REQUIRE_ERRNO(ENOENT, realpath("", resb) == NULL); + ATF_REQUIRE_STREQ("", resb); +} + +ATF_TC(realpath_buffer_overflow); +ATF_TC_HEAD(realpath_buffer_overflow, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test for out of bounds read from 'left' array " + "(compile realpath.c with '-fsanitize=address')"); +} + +ATF_TC_BODY(realpath_buffer_overflow, tc) +{ + char path[PATH_MAX] = ""; + char resb[PATH_MAX] = ""; + + memset(path, 'a', sizeof(path) - 1); + path[1] = '/'; + ATF_REQUIRE(realpath(path, resb) == NULL); +} + +ATF_TC(realpath_empty_symlink); +ATF_TC_HEAD(realpath_empty_symlink, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test for correct behavior when encountering empty symlinks"); +} + +ATF_TC_BODY(realpath_empty_symlink, tc) +{ + char path[PATH_MAX] = ""; + char slnk[PATH_MAX] = ""; + char resb[PATH_MAX] = ""; + int fd; + + (void)strlcat(slnk, "empty_symlink", sizeof(slnk)); + + ATF_REQUIRE(symlink("", slnk) == 0); + + fd = open("aaa", O_RDONLY | O_CREAT, 0600); + + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE(close(fd) == 0); + + (void)strlcat(path, "empty_symlink", sizeof(path)); + (void)strlcat(path, "/aaa", sizeof(path)); + + ATF_REQUIRE_ERRNO(ENOENT, realpath(path, resb) == NULL); + + ATF_REQUIRE(unlink("aaa") == 0); + ATF_REQUIRE(unlink(slnk) == 0); +} + +ATF_TC(realpath_partial); +ATF_TC_HEAD(realpath_partial, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test that failure leaves a partial result"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} + +ATF_TC_BODY(realpath_partial, tc) +{ + char resb[PATH_MAX] = ""; + size_t len; + + /* scenario 1: missing directory */ + ATF_REQUIRE_EQ(0, mkdir("foo", 0755)); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 8 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/bar", resb + len - 8); + + /* scenario 2: dead link 1 */ + ATF_REQUIRE_EQ(0, symlink("nix", "foo/bar")); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 8 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/nix", resb + len - 8); + + /* scenario 3: missing file */ + ATF_REQUIRE_EQ(0, unlink("foo/bar")); + ATF_REQUIRE_EQ(0, mkdir("foo/bar", 0755)); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 12 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/bar/baz", resb + len - 12); + + /* scenario 4: dead link 2 */ + ATF_REQUIRE_EQ(0, symlink("nix", "foo/bar/baz")); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 12 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/bar/nix", resb + len - 12); + + /* scenario 5: unreadable directory */ + ATF_REQUIRE_EQ(0, chmod("foo", 000)); + ATF_REQUIRE_ERRNO(EACCES, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 4 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo", resb + len - 4); + + /* scenario 6: not a directory */ + ATF_REQUIRE_EQ(0, close(creat("bar", 0644))); + ATF_REQUIRE_ERRNO(ENOTDIR, realpath("bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 4 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/bar", resb + len - 4); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, realpath_null); + ATF_TP_ADD_TC(tp, realpath_empty); + ATF_TP_ADD_TC(tp, realpath_buffer_overflow); + ATF_TP_ADD_TC(tp, realpath_empty_symlink); + ATF_TP_ADD_TC(tp, realpath_partial); + + return atf_no_error(); +} diff --git a/lib/libc/tests/gen/scandir_blocks_test.c b/lib/libc/tests/gen/scandir_blocks_test.c new file mode 100644 index 000000000000..b94270bc410e --- /dev/null +++ b/lib/libc/tests/gen/scandir_blocks_test.c @@ -0,0 +1,118 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <dirent.h> +#include <fcntl.h> +#include <stdlib.h> + +#include <atf-c.h> + +static void +scandir_blocks_prepare(const struct atf_tc *tc) +{ + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE_EQ(0, mkdir("dir/dir", 0755)); + ATF_REQUIRE_EQ(0, close(creat("dir/file", 0644))); + ATF_REQUIRE_EQ(0, symlink("file", "dir/link")); + ATF_REQUIRE_EQ(0, mkdir("dir/skip", 0755)); +} + +static void +scandir_blocks_verify(const struct atf_tc *tc, int n, struct dirent **namelist) +{ + ATF_REQUIRE_EQ_MSG(5, n, "return value is %d", n); + ATF_CHECK_STREQ("link", namelist[0]->d_name); + ATF_CHECK_STREQ("file", namelist[1]->d_name); + ATF_CHECK_STREQ("dir", namelist[2]->d_name); + ATF_CHECK_STREQ("..", namelist[3]->d_name); + ATF_CHECK_STREQ(".", namelist[4]->d_name); +} + +ATF_TC(scandir_b_test); +ATF_TC_HEAD(scandir_b_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test scandir_b()"); +} +ATF_TC_BODY(scandir_b_test, tc) +{ + struct dirent **namelist = NULL; + int i, ret; + + scandir_blocks_prepare(tc); + ret = scandir_b("dir", &namelist, + ^(const struct dirent *ent) { + return (strcmp(ent->d_name, "skip") != 0); + }, + ^(const struct dirent **a, const struct dirent **b) { + return (strcmp((*b)->d_name, (*a)->d_name)); + }); + scandir_blocks_verify(tc, ret, namelist); + for (i = 0; i < ret; i++) + free(namelist[i]); + free(namelist); +} + +ATF_TC(fdscandir_b_test); +ATF_TC_HEAD(fdscandir_b_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test fdscandir_b()"); +} +ATF_TC_BODY(fdscandir_b_test, tc) +{ + struct dirent **namelist = NULL; + int fd, i, ret; + + scandir_blocks_prepare(tc); + ATF_REQUIRE((fd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0); + ret = fdscandir_b(fd, &namelist, + ^(const struct dirent *ent) { + return (strcmp(ent->d_name, "skip") != 0); + }, + ^(const struct dirent **a, const struct dirent **b) { + return (strcmp((*b)->d_name, (*a)->d_name)); + }); + scandir_blocks_verify(tc, ret, namelist); + for (i = 0; i < ret; i++) + free(namelist[i]); + free(namelist); + ATF_REQUIRE_EQ(0, close(fd)); +} + +ATF_TC(scandirat_b_test); +ATF_TC_HEAD(scandirat_b_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test scandirat_b()"); +} +ATF_TC_BODY(scandirat_b_test, tc) +{ + struct dirent **namelist = NULL; + int fd, i, ret; + + scandir_blocks_prepare(tc); + ATF_REQUIRE((fd = open("dir", O_DIRECTORY | O_SEARCH)) >= 0); + ret = scandirat_b(fd, ".", &namelist, + ^(const struct dirent *ent) { + return (strcmp(ent->d_name, "skip") != 0); + }, + ^(const struct dirent **a, const struct dirent **b) { + return (strcmp((*b)->d_name, (*a)->d_name)); + }); + scandir_blocks_verify(tc, ret, namelist); + for (i = 0; i < ret; i++) + free(namelist[i]); + free(namelist); + ATF_REQUIRE_EQ(0, close(fd)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, scandir_b_test); + ATF_TP_ADD_TC(tp, fdscandir_b_test); + ATF_TP_ADD_TC(tp, scandirat_b_test); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/scandir_test.c b/lib/libc/tests/gen/scandir_test.c new file mode 100644 index 000000000000..afd25bf7c0b2 --- /dev/null +++ b/lib/libc/tests/gen/scandir_test.c @@ -0,0 +1,195 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +static void +scandir_prepare(const struct atf_tc *tc) +{ + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + ATF_REQUIRE_EQ(0, mkdir("dir/dir", 0755)); + ATF_REQUIRE_EQ(0, close(creat("dir/file", 0644))); + ATF_REQUIRE_EQ(0, symlink("file", "dir/link")); + ATF_REQUIRE_EQ(0, mkdir("dir/skip", 0755)); +} + +static void +scandir_verify(const struct atf_tc *tc, int n, struct dirent **namelist) +{ + ATF_REQUIRE_EQ_MSG(5, n, "return value is %d", n); + ATF_CHECK_STREQ("link", namelist[0]->d_name); + ATF_CHECK_STREQ("file", namelist[1]->d_name); + ATF_CHECK_STREQ("dir", namelist[2]->d_name); + ATF_CHECK_STREQ("..", namelist[3]->d_name); + ATF_CHECK_STREQ(".", namelist[4]->d_name); +} + +static int +scandir_select(const struct dirent *ent) +{ + return (strcmp(ent->d_name, "skip") != 0); +} + +static int +scandir_compare(const struct dirent **a, const struct dirent **b) +{ + return (strcmp((*b)->d_name, (*a)->d_name)); +} + +ATF_TC(scandir_test); +ATF_TC_HEAD(scandir_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test scandir()"); +} +ATF_TC_BODY(scandir_test, tc) +{ + struct dirent **namelist = NULL; + int i, ret; + + scandir_prepare(tc); + ret = scandir("dir", &namelist, scandir_select, scandir_compare); + scandir_verify(tc, ret, namelist); + for (i = 0; i < ret; i++) + free(namelist[i]); + free(namelist); +} + +ATF_TC(fdscandir_test); +ATF_TC_HEAD(fdscandir_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test fdscandir()"); +} +ATF_TC_BODY(fdscandir_test, tc) +{ + struct dirent **namelist = NULL; + int fd, i, ret; + + scandir_prepare(tc); + ATF_REQUIRE((fd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0); + ret = fdscandir(fd, &namelist, scandir_select, scandir_compare); + scandir_verify(tc, ret, namelist); + for (i = 0; i < ret; i++) + free(namelist[i]); + free(namelist); + ATF_REQUIRE_EQ(0, close(fd)); +} + +ATF_TC(scandirat_test); +ATF_TC_HEAD(scandirat_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test scandirat()"); +} +ATF_TC_BODY(scandirat_test, tc) +{ + struct dirent **namelist = NULL; + int fd, i, ret; + + scandir_prepare(tc); + ATF_REQUIRE((fd = open("dir", O_DIRECTORY | O_SEARCH)) >= 0); + ret = scandirat(fd, ".", &namelist, scandir_select, scandir_compare); + scandir_verify(tc, ret, namelist); + for (i = 0; i < ret; i++) + free(namelist[i]); + free(namelist); + ATF_REQUIRE_EQ(0, close(fd)); +} + +static int +scandir_none(const struct dirent *ent __unused) +{ + return (0); +} + +ATF_TC(scandir_none); +ATF_TC_HEAD(scandir_none, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test scandir() when no entries are selected"); +} +ATF_TC_BODY(scandir_none, tc) +{ + struct dirent **namelist = NULL; + + ATF_REQUIRE_EQ(0, scandir(".", &namelist, scandir_none, alphasort)); + ATF_REQUIRE(namelist); + free(namelist); +} + +/* + * Test that scandir() propagates errors from readdir(): we create a + * directory with enough entries that it can't be read in a single + * getdirentries() call, then abuse the selection callback to close the + * file descriptor scandir() is using after the first call, causing the + * next one to fail, and verify that readdir() returns an error instead of + * a partial result. We make two passes, one in which nothing was + * selected before the error occurred, and one in which everything was. + */ +static int scandir_error_count; +static int scandir_error_fd; +static int scandir_error_select_return; + +static int +scandir_error_select(const struct dirent *ent __unused) +{ + if (scandir_error_count++ == 0) + close(scandir_error_fd); + return (scandir_error_select_return); +} + +ATF_TC(scandir_error); +ATF_TC_HEAD(scandir_error, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test that scandir() propagates errors from readdir()"); +} +ATF_TC_BODY(scandir_error, tc) +{ + char path[16]; + struct dirent **namelist = NULL; + int fd, i; + + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + for (i = 0; i < 1024; i++) { + snprintf(path, sizeof(path), "dir/%04x", i); + ATF_REQUIRE_EQ(0, symlink(path + 4, path)); + } + + /* first pass, select nothing */ + ATF_REQUIRE((fd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0); + scandir_error_count = 0; + scandir_error_fd = fd; + scandir_error_select_return = 0; + ATF_CHECK_ERRNO(EBADF, + fdscandir(fd, &namelist, scandir_error_select, NULL) < 0); + ATF_CHECK_EQ(NULL, namelist); + + /* second pass, select everything */ + ATF_REQUIRE((fd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0); + scandir_error_count = 0; + scandir_error_fd = fd; + scandir_error_select_return = 1; + ATF_CHECK_ERRNO(EBADF, + fdscandir(fd, &namelist, scandir_error_select, NULL) < 0); + ATF_CHECK_EQ(NULL, namelist); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, scandir_test); + ATF_TP_ADD_TC(tp, fdscandir_test); + ATF_TP_ADD_TC(tp, scandirat_test); + ATF_TP_ADD_TC(tp, scandir_none); + ATF_TP_ADD_TC(tp, scandir_error); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/sig2str_test.c b/lib/libc/tests/gen/sig2str_test.c new file mode 100644 index 000000000000..00b6ebb2349a --- /dev/null +++ b/lib/libc/tests/gen/sig2str_test.c @@ -0,0 +1,213 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 Ricardo Branco <rbranco@suse.de> + * 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 AUTHOR 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 AUTHOR 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 <ctype.h> +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <string.h> + +#include <atf-c.h> + +static void +test_roundtrip(int signum) +{ + char str[SIG2STR_MAX]; + int sig; + + ATF_REQUIRE_EQ_MSG(sig2str(signum, str), 0, + "sig2str(%d) failed", signum); + ATF_REQUIRE_EQ_MSG(str2sig(str, &sig), 0, + "str2sig(\"%s\") failed", str); + ATF_REQUIRE_INTEQ_MSG(sig, signum, + "Mismatch: roundtrip conversion gave %d instead of %d", + sig, signum); +} + +ATF_TC_WITHOUT_HEAD(sig2str_valid); +ATF_TC_BODY(sig2str_valid, tc) +{ + int sig; + + for (sig = 1; sig < sys_nsig; sig++) { + test_roundtrip(sig); + } +} + +ATF_TC_WITHOUT_HEAD(sig2str_invalid); +ATF_TC_BODY(sig2str_invalid, tc) +{ + char buf[SIG2STR_MAX]; + + ATF_CHECK(sig2str(0, buf) != 0); + ATF_CHECK(sig2str(-1, buf) != 0); + ATF_CHECK(sig2str(SIGRTMAX + 1, buf) != 0); +} + +ATF_TC_WITHOUT_HEAD(str2sig_rtmin_rtmax); +ATF_TC_BODY(str2sig_rtmin_rtmax, tc) +{ + int sig; + + ATF_CHECK_MSG(str2sig("RTMIN", &sig) == 0, + "str2sig(\"RTMIN\") failed"); + ATF_CHECK_INTEQ_MSG(sig, SIGRTMIN, + "RTMIN mapped to %d, expected %d", sig, SIGRTMIN); + + ATF_CHECK_MSG(str2sig("RTMAX", &sig) == 0, + "str2sig(\"RTMAX\") failed"); + ATF_CHECK_INTEQ_MSG(sig, SIGRTMAX, + "RTMAX mapped to %d, expected %d", sig, SIGRTMAX); + + ATF_CHECK_MSG(str2sig("RTMIN+1", &sig) == 0, + "str2sig(\"RTMIN+1\") failed"); + ATF_CHECK_INTEQ_MSG(sig, SIGRTMIN + 1, + "RTMIN+1 mapped to %d, expected %d", sig, SIGRTMIN + 1); + + ATF_CHECK_MSG(str2sig("RTMAX-1", &sig) == 0, + "str2sig(\"RTMAX-1\") failed"); + ATF_CHECK_INTEQ_MSG(sig, SIGRTMAX - 1, + "RTMAX-1 mapped to %d, expected %d", sig, SIGRTMAX - 1); +} + +ATF_TC_WITHOUT_HEAD(str2sig_invalid_rt); +ATF_TC_BODY(str2sig_invalid_rt, tc) +{ + int i, sig; + + const char *invalid[] = { + "RTMIN+0", + "RTMAX-0", + "RTMIN-777", + "RTMIN+777", + "RTMAX-777", + "RTMAX+777", + "RTMIN-", + "RTMAX-", + "RTMIN0", + "RTMAX1", + "RTMIN+abc", + "RTMIN-abc", + NULL + }; + + for (i = 0; invalid[i] != NULL; i++) { + ATF_CHECK_MSG(str2sig(invalid[i], &sig) != 0, + "str2sig(\"%s\") unexpectedly succeeded", invalid[i]); + } +} + +ATF_TC_WITHOUT_HEAD(str2sig_fullname); +ATF_TC_BODY(str2sig_fullname, tc) +{ + char fullname[SIG2STR_MAX + 3]; + int n, sig; + + for (sig = 1; sig < sys_nsig; sig++) { + snprintf(fullname, sizeof(fullname), "SIG%s", sys_signame[sig]); + + ATF_CHECK_MSG(str2sig(fullname, &n) == 0, + "str2sig(\"%s\") failed with errno %d (%s)", + fullname, errno, strerror(errno)); + + ATF_CHECK_INTEQ_MSG(n, sig, + "Mismatch: %s = %d, str2sig(\"%s\") = %d", + sys_signame[sig], sig, fullname, n); + } +} + +ATF_TC_WITHOUT_HEAD(str2sig_lowercase); +ATF_TC_BODY(str2sig_lowercase, tc) +{ + char fullname[SIG2STR_MAX + 3]; + int n, sig; + + for (sig = 1; sig < sys_nsig; sig++) { + snprintf(fullname, sizeof(fullname), "sig%s", sys_signame[sig]); + for (size_t i = 3; i < strlen(fullname); i++) + fullname[i] = toupper((unsigned char)fullname[i]); + + ATF_CHECK_MSG(str2sig(fullname, &n) == 0, + "str2sig(\"%s\") failed with errno %d (%s)", + fullname, errno, strerror(errno)); + + ATF_CHECK_INTEQ_MSG(n, sig, + "Mismatch: %s = %d, str2sig(\"%s\") = %d", + sys_signame[sig], sig, fullname, n); + } +} + +ATF_TC_WITHOUT_HEAD(str2sig_numeric); +ATF_TC_BODY(str2sig_numeric, tc) +{ + char buf[16]; + int n, sig; + + for (sig = NSIG; sig < SIGRTMIN; sig++) { + snprintf(buf, sizeof(buf), "%d", sig); + ATF_CHECK_MSG(str2sig(buf, &n) == 0, + "str2sig(\"%s\") failed", buf); + ATF_CHECK_INTEQ_MSG(n, sig, + "Mismatch: str2sig(\"%s\") = %d, expected %d", + buf, n, sig); + } +} + +ATF_TC_WITHOUT_HEAD(str2sig_invalid); +ATF_TC_BODY(str2sig_invalid, tc) +{ + const char *invalid[] = { + "SIGDOESNOTEXIST", + "DOESNOTEXIST", + "INTERRUPT", + "", + "SIG", + "123abc", + "sig1extra", + NULL + }; + int i, sig; + + for (i = 0; invalid[i] != NULL; i++) { + ATF_CHECK_MSG(str2sig(invalid[i], &sig) != 0, + "str2sig(\"%s\") unexpectedly succeeded", invalid[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, sig2str_valid); + ATF_TP_ADD_TC(tp, sig2str_invalid); + ATF_TP_ADD_TC(tp, str2sig_rtmin_rtmax); + ATF_TP_ADD_TC(tp, str2sig_invalid_rt); + ATF_TP_ADD_TC(tp, str2sig_fullname); + ATF_TP_ADD_TC(tp, str2sig_lowercase); + ATF_TP_ADD_TC(tp, str2sig_numeric); + ATF_TP_ADD_TC(tp, str2sig_invalid); + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/sigsetops_test.c b/lib/libc/tests/gen/sigsetops_test.c new file mode 100644 index 000000000000..a22c4b3f4f59 --- /dev/null +++ b/lib/libc/tests/gen/sigsetops_test.c @@ -0,0 +1,188 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org> + * 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 AUTHOR 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 AUTHOR 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 <errno.h> +#include <signal.h> +#include <stdbool.h> + +#include <atf-c.h> + +/* Return the status of the specified sig's bit. */ +static bool +sigbitstatus(const sigset_t *set, int sig) +{ + + return (set->__bits[_SIG_WORD(sig)] & _SIG_BIT(sig)) != 0; +} + +/* Verify that sig is the lone bit set in the sigset. */ +static void +siglonebit(const sigset_t *set, int sig) +{ + int i; + + for (i = 0; i < _SIG_WORDS; ++i) { + if (i != _SIG_WORD(sig)) + ATF_REQUIRE_MSG(set->__bits[i] == 0, + "word %d altered to %x", i, set->__bits[i]); + else + ATF_REQUIRE_MSG((set->__bits[i] & ~_SIG_BIT(sig)) == 0, + "word %d has other bits set: %x", i, + set->__bits[i] & ~_SIG_BIT(sig)); + } +} + +static void +sigcompare(const sigset_t *left, const sigset_t *right) +{ + int i; + + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(left->__bits[i] == right->__bits[i], + "sig comparison failed at %d; left=%x, right=%x", + i, left->__bits[i], right->__bits[i]); + } +} + +/* + * Test implementation details of our sigsetops... make sure the correct bits + * are getting set, for the most part, and that duplicate operations don't + * error out. + */ +ATF_TC_WITHOUT_HEAD(posix_sigsetop_test); +ATF_TC_BODY(posix_sigsetop_test, tc) +{ + sigset_t set; + int i; + + ATF_REQUIRE(sigfillset(&set) == 0); + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(set.__bits[i] == ~0U, "sigfillset failed @ %d", + i); + } + ATF_REQUIRE(sigemptyset(&set) == 0); + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(set.__bits[i] == 0, "sigemptyset failed @ %d", + i); + } + /* Ensure that sigismember reflects the empty set status. */ + for (i = 1; i < NSIG; ++i) { + ATF_REQUIRE(sigismember(&set, i) == 0); + } + + ATF_REQUIRE(sigaddset(&set, -1) == -1 && errno == EINVAL); + ATF_REQUIRE(sigaddset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL); + ATF_REQUIRE(sigdelset(&set, -1) == -1 && errno == EINVAL); + ATF_REQUIRE(sigdelset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL); + + ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0); + ATF_REQUIRE(sigismember(&set, SIGSEGV) != 0); + ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), "sigaddset failure"); + siglonebit(&set, SIGSEGV); + + /* + * A second addition should succeed without altering the state. This + * should be trivially true. + */ + ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0); + ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), + "sigaddset twice changed bit"); + + ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0); + ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), "sigdelset failure"); + ATF_REQUIRE(sigismember(&set, SIGSEGV) == 0); + ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0); + ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), + "sigdelset twice changed bit"); + for (i = 0; i < _SIG_WORDS; ++i) { + ATF_REQUIRE_MSG(set.__bits[i] == 0, "set not empty @ %d", + i); + } + for (i = 1; i < NSIG; ++i) { + ATF_REQUIRE(sigismember(&set, i) == 0); + } +} + +/* + * Test extended sigset ops for union/intersection and testing of empty set. + */ +ATF_TC_WITHOUT_HEAD(extended_sigsetop_test); +ATF_TC_BODY(extended_sigsetop_test, tc) +{ + sigset_t chkset, set1, set2, set3; + + sigemptyset(&chkset); + sigemptyset(&set1); + sigemptyset(&set2); + ATF_REQUIRE(sigisemptyset(&chkset) != 0); + sigaddset(&set1, SIGSEGV); + sigaddset(&set2, SIGKILL); + sigaddset(&chkset, SIGSEGV); + ATF_REQUIRE(sigisemptyset(&chkset) == 0); + sigaddset(&chkset, SIGKILL); + ATF_REQUIRE(sigorset(&set3, &set1, &set2) == 0); + ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV)); + ATF_REQUIRE(sigbitstatus(&set3, SIGKILL)); + + /* + * chkset was built with our POSIX-specified set operations that we've + * already tested, so it's a good comparison. + */ + sigcompare(&chkset, &set3); + /* + * Clear chkset; make sure sigisemptyset() still looks ok. sigaddset + * and sigdelset have already been tested to make sure that they're not + * touching other bits. + */ + sigdelset(&chkset, SIGSEGV); + sigdelset(&chkset, SIGKILL); + ATF_REQUIRE(sigisemptyset(&chkset) != 0); + ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0); + /* Make sure we clobbered these. */ + ATF_REQUIRE(!sigbitstatus(&set3, SIGSEGV)); + ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL)); + ATF_REQUIRE(sigisemptyset(&set3) != 0); + /* Rebuild for sigandset test */ + sigemptyset(&set1); + sigemptyset(&set2); + sigaddset(&set1, SIGSEGV); + sigaddset(&set1, SIGKILL); + sigaddset(&set2, SIGSEGV); + ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0); + ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV)); + ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL)); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, posix_sigsetop_test); + ATF_TP_ADD_TC(tp, extended_sigsetop_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/spawnp_enoexec.sh b/lib/libc/tests/gen/spawnp_enoexec.sh new file mode 100755 index 000000000000..1050b7a6f944 --- /dev/null +++ b/lib/libc/tests/gen/spawnp_enoexec.sh @@ -0,0 +1,3 @@ +# Intentionally no interpreter + +exit 42 diff --git a/lib/libc/tests/gen/test-fnmatch.c b/lib/libc/tests/gen/test-fnmatch.c new file mode 100644 index 000000000000..1a6c6ed7efdf --- /dev/null +++ b/lib/libc/tests/gen/test-fnmatch.c @@ -0,0 +1,99 @@ +/*- + * Copyright (c) 2010 Jilles Tjoelker + * 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 AUTHOR 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 AUTHOR 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/param.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "fnmatch_testcases.h" + +static int +write_sh_tests(const char *progname, int num) +{ + size_t i; + struct testcase *t; + + printf("# Generated by %s -s %d, do not edit.\n", progname, num); + printf("# $" "FreeBSD$\n"); + printf("failures=\n"); + printf("failed() { printf '%%s\\n' \"Failed: $1 '$2' '$3'\"; failures=x$failures; }\n"); + if (num == 1) { + printf("testmatch() { eval \"case \\$2 in ''$1) ;; *) failed testmatch \\\"\\$@\\\";; esac\"; }\n"); + printf("testnomatch() { eval \"case \\$2 in ''$1) failed testnomatch \\\"\\$@\\\";; esac\"; }\n"); + } else if (num == 2) { + printf("# We do not treat a backslash specially in this case,\n"); + printf("# but this is not the case in all shells.\n"); + printf("netestmatch() { case $2 in $1) ;; *) failed netestmatch \"$@\";; esac; }\n"); + printf("netestnomatch() { case $2 in $1) failed netestnomatch \"$@\";; esac; }\n"); + } + + for (i = 0; i < nitems(testcases); i++) { + t = &testcases[i]; + if (strchr(t->pattern, '\'') != NULL || + strchr(t->string, '\'') != NULL) + continue; + if (t->flags == 0 && strcmp(t->pattern, "\\") == 0) + continue; + if (num == 1 && t->flags == 0) + printf("test%smatch '%s' '%s'\n", + t->result == FNM_NOMATCH ? "no" : "", + t->pattern, t->string); + if (num == 2 && (t->flags == FNM_NOESCAPE || + (t->flags == 0 && strchr(t->pattern, '\\') == NULL))) + printf("netest%smatch '%s' '%s'\n", + t->result == FNM_NOMATCH ? "no" : "", + t->pattern, t->string); + } + printf("[ -z \"$failures\" ]\n"); + return 0; +} + +static void +usage(char *progname) +{ + fprintf(stderr, "usage: %s [-s num]\n", progname); + fprintf(stderr, "-s option writes tests for sh(1), num is 1 or 2\n"); +} + +int +main(int argc, char *argv[]) +{ + int opt; + + while ((opt = getopt(argc, argv, "s:")) != -1) { + switch (opt) { + case 's': + return (write_sh_tests(argv[0], atoi(optarg))); + default: + usage(argv[0]); + exit(1); + } + } + usage(argv[0]); + exit(1); +} diff --git a/lib/libc/tests/gen/wordexp_test.c b/lib/libc/tests/gen/wordexp_test.c new file mode 100644 index 000000000000..a8b9d5509633 --- /dev/null +++ b/lib/libc/tests/gen/wordexp_test.c @@ -0,0 +1,383 @@ +/*- + * Copyright (c) 2003 Tim J. Robbins + * 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 AUTHOR 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 AUTHOR 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. + */ + +/* + * Test program for wordexp() and wordfree() as specified by + * IEEE Std. 1003.1-2001. + */ + +#include <sys/wait.h> +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wordexp.h> + +#include <atf-c.h> + +static void +chld_handler(int x) +{ + int status, serrno; + + (void)x; + serrno = errno; + while (waitpid(-1, &status, WNOHANG) > 0) + ; + errno = serrno; +} + +ATF_TC_WITHOUT_HEAD(simple_test); +ATF_TC_BODY(simple_test, tc) +{ + wordexp_t we; + int r; + + /* Test that the macros are there. */ + (void)(WRDE_APPEND + WRDE_DOOFFS + WRDE_NOCMD + WRDE_REUSE + + WRDE_SHOWERR + WRDE_UNDEF); + (void)(WRDE_BADCHAR + WRDE_BADVAL + WRDE_CMDSUB + WRDE_NOSPACE + + WRDE_SYNTAX); + + /* Simple test. */ + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(long_output_test); +ATF_TC_BODY(long_output_test, tc) +{ + char longdata[6 * 10000 + 1]; + wordexp_t we; + int i, r; + + /* Long output. */ + for (i = 0; i < 10000; i++) + snprintf(longdata + 6 * i, 7, "%05d ", i); + r = wordexp(longdata, &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 10000); + ATF_REQUIRE(we.we_wordv[10000] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_DOOFFS_test); +ATF_TC_BODY(WRDE_DOOFFS_test, tc) +{ + wordexp_t we; + int r; + + we.we_offs = 3; + r = wordexp("hello world", &we, WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(we.we_wordv[0] == NULL); + ATF_REQUIRE(we.we_wordv[1] == NULL); + ATF_REQUIRE(we.we_wordv[2] == NULL); + ATF_REQUIRE(strcmp(we.we_wordv[3], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[4], "world") == 0); + ATF_REQUIRE(we.we_wordv[5] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_REUSE_test); +ATF_TC_BODY(WRDE_REUSE_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("hello world", &we, 0); + r = wordexp("hello world", &we, WRDE_REUSE); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_APPEND_test); +ATF_TC_BODY(WRDE_APPEND_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("this is", &we, 0); + ATF_REQUIRE(r == 0); + r = wordexp("a test", &we, WRDE_APPEND); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 4); + ATF_REQUIRE(strcmp(we.we_wordv[0], "this") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "is") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[2], "a") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[3], "test") == 0); + ATF_REQUIRE(we.we_wordv[4] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_DOOFFS__WRDE_APPEND_test); +ATF_TC_BODY(WRDE_DOOFFS__WRDE_APPEND_test, tc) +{ + wordexp_t we; + int r; + + we.we_offs = 2; + r = wordexp("this is", &we, WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + r = wordexp("a test", &we, WRDE_APPEND|WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + r = wordexp("of wordexp", &we, WRDE_APPEND|WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 6); + ATF_REQUIRE(we.we_wordv[0] == NULL); + ATF_REQUIRE(we.we_wordv[1] == NULL); + ATF_REQUIRE(strcmp(we.we_wordv[2], "this") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[3], "is") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[4], "a") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[5], "test") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[6], "of") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[7], "wordexp") == 0); + ATF_REQUIRE(we.we_wordv[8] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_UNDEF_test); +ATF_TC_BODY(WRDE_UNDEF_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("${dont_set_me}", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_BADVAL); +} + +ATF_TC_WITHOUT_HEAD(WRDE_NOCMD_test); +ATF_TC_BODY(WRDE_NOCMD_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("`date`", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("\"`date`\"", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("$(date)", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("\"$(date)\"", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("$((3+5))", &we, WRDE_NOCMD); + ATF_REQUIRE(r == 0); + r = wordexp("\\$\\(date\\)", &we, WRDE_NOCMD|WRDE_REUSE); + ATF_REQUIRE(r == 0); + r = wordexp("'`date`'", &we, WRDE_NOCMD|WRDE_REUSE); + ATF_REQUIRE(r == 0); + r = wordexp("'$(date)'", &we, WRDE_NOCMD|WRDE_REUSE); + ATF_REQUIRE(r == 0); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_BADCHAR_test); +ATF_TC_BODY(WRDE_BADCHAR_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("'\n|&;<>(){}'", &we, 0); + ATF_REQUIRE(r == 0); + r = wordexp("\"\n|&;<>(){}\"", &we, WRDE_REUSE); + ATF_REQUIRE(r == 0); + r = wordexp("\\\n\\|\\&\\;\\<\\>\\(\\)\\{\\}", &we, WRDE_REUSE); + ATF_REQUIRE(r == 0); + wordfree(&we); + r = wordexp("test \n test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test | test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test & test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test ; test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test > test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test < test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test ( test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test ) test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test { test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test } test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); +} + +ATF_TC_WITHOUT_HEAD(WRDE_SYNTAX_test); +ATF_TC_BODY(WRDE_SYNTAX_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("'", &we, 0); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("'", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("'\\'", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 1); + ATF_REQUIRE(strcmp(we.we_wordv[0], "\\") == 0); + ATF_REQUIRE(we.we_wordv[1] == NULL); + wordfree(&we); + /* Two syntax errors that are not detected by the current we_check(). */ + r = wordexp("${IFS:+'}", &we, 0); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("${IFS:+'}", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("$(case)", &we, 0); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("$(case)", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_SYNTAX); +} + +ATF_TC_WITHOUT_HEAD(with_SIGCHILD_handler_test); +ATF_TC_BODY(with_SIGCHILD_handler_test, tc) +{ + struct sigaction sa; + wordexp_t we; + int r; + + /* With a SIGCHLD handler that reaps all zombies. */ + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sa.sa_handler = chld_handler; + r = sigaction(SIGCHLD, &sa, NULL); + ATF_REQUIRE(r == 0); + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + sa.sa_handler = SIG_DFL; + r = sigaction(SIGCHLD, &sa, NULL); + ATF_REQUIRE(r == 0); +} + +ATF_TC_WITHOUT_HEAD(with_SIGCHILD_ignore_test); +ATF_TC_BODY(with_SIGCHILD_ignore_test, tc) +{ + struct sigaction sa; + wordexp_t we; + int r; + + /* With SIGCHLD set to ignore so that the kernel auto-reaps zombies. */ + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sa.sa_handler = SIG_IGN; + r = sigaction(SIGCHLD, &sa, NULL); + ATF_REQUIRE(r == 0); + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + sa.sa_handler = SIG_DFL; + r = sigaction(SIGCHLD, &sa, NULL); + ATF_REQUIRE(r == 0); +} + +ATF_TC_WITHOUT_HEAD(with_unused_non_default_IFS_test); +ATF_TC_BODY(with_unused_non_default_IFS_test, tc) +{ + wordexp_t we; + int r; + + /* + * With IFS set to a non-default value (without depending on whether + * IFS is inherited or not). + */ + r = setenv("IFS", ":", 1); + ATF_REQUIRE(r == 0); + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + r = unsetenv("IFS"); + ATF_REQUIRE(r == 0); +} + +ATF_TC_WITHOUT_HEAD(with_used_non_default_IFS_test); +ATF_TC_BODY(with_used_non_default_IFS_test, tc) +{ + wordexp_t we; + int r; + + /* + * With IFS set to a non-default value, and using it. + */ + r = setenv("IFS", ":", 1); + ATF_REQUIRE(r == 0); + r = wordexp("${IFS+hello:world}", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + r = unsetenv("IFS"); + ATF_REQUIRE(r == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, simple_test); + ATF_TP_ADD_TC(tp, long_output_test); + ATF_TP_ADD_TC(tp, WRDE_DOOFFS_test); + ATF_TP_ADD_TC(tp, WRDE_REUSE_test); + ATF_TP_ADD_TC(tp, WRDE_APPEND_test); + ATF_TP_ADD_TC(tp, WRDE_DOOFFS__WRDE_APPEND_test); + ATF_TP_ADD_TC(tp, WRDE_UNDEF_test); + ATF_TP_ADD_TC(tp, WRDE_NOCMD_test); + ATF_TP_ADD_TC(tp, WRDE_BADCHAR_test); + ATF_TP_ADD_TC(tp, WRDE_SYNTAX_test); + ATF_TP_ADD_TC(tp, with_SIGCHILD_handler_test); + ATF_TP_ADD_TC(tp, with_SIGCHILD_ignore_test); + ATF_TP_ADD_TC(tp, with_unused_non_default_IFS_test); + ATF_TP_ADD_TC(tp, with_used_non_default_IFS_test); + + return (atf_no_error()); +} |