diff options
Diffstat (limited to 'lib/libutil/tests')
-rw-r--r-- | lib/libutil/tests/Makefile | 14 | ||||
-rw-r--r-- | lib/libutil/tests/Makefile.depend | 18 | ||||
-rw-r--r-- | lib/libutil/tests/cpuset_test.c | 71 | ||||
-rw-r--r-- | lib/libutil/tests/expand_number_test.c | 294 | ||||
-rw-r--r-- | lib/libutil/tests/flopen_test.c | 207 | ||||
-rw-r--r-- | lib/libutil/tests/forkpty_test.c | 58 | ||||
-rw-r--r-- | lib/libutil/tests/grp_test.c | 114 | ||||
-rw-r--r-- | lib/libutil/tests/humanize_number_test.c | 592 | ||||
-rw-r--r-- | lib/libutil/tests/pidfile_test.c | 326 | ||||
-rw-r--r-- | lib/libutil/tests/trimdomain-nodomain_test.c | 90 | ||||
-rw-r--r-- | lib/libutil/tests/trimdomain_test.c | 90 |
11 files changed, 1874 insertions, 0 deletions
diff --git a/lib/libutil/tests/Makefile b/lib/libutil/tests/Makefile new file mode 100644 index 000000000000..cdcf3466aec8 --- /dev/null +++ b/lib/libutil/tests/Makefile @@ -0,0 +1,14 @@ +TAP_TESTS_C+= flopen_test +TAP_TESTS_C+= grp_test +TAP_TESTS_C+= humanize_number_test +TAP_TESTS_C+= pidfile_test +TAP_TESTS_C+= trimdomain_test +TAP_TESTS_C+= trimdomain-nodomain_test +ATF_TESTS_C+= cpuset_test +ATF_TESTS_C+= expand_number_test +ATF_TESTS_C+= forkpty_test + +WARNS?= 2 +LIBADD+= util + +.include <bsd.test.mk> diff --git a/lib/libutil/tests/Makefile.depend b/lib/libutil/tests/Makefile.depend new file mode 100644 index 000000000000..1eb6d2ed64a8 --- /dev/null +++ b/lib/libutil/tests/Makefile.depend @@ -0,0 +1,18 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + 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/libutil/tests/cpuset_test.c b/lib/libutil/tests/cpuset_test.c new file mode 100644 index 000000000000..0c3c51eac054 --- /dev/null +++ b/lib/libutil/tests/cpuset_test.c @@ -0,0 +1,71 @@ +#include <sys/cdefs.h> +#include <sys/types.h> +#include <sys/cpuset.h> + +#include <stdio.h> +#include <libutil.h> +#include <atf-c.h> + +ATF_TC(invalid); +ATF_TC_HEAD(invalid, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test invalid cpu"); +} + +ATF_TC_BODY(invalid, tc) +{ + cpuset_t mask; + char testvalue[BUFSIZ]; + + snprintf(testvalue, sizeof(testvalue), "%d", CPU_SETSIZE + 1); + + ATF_CHECK_EQ(cpuset_parselist(testvalue, &mask), CPUSET_PARSE_INVALID_CPU); +} + +ATF_TC(invalidchar); +ATF_TC_HEAD(invalidchar, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test invalid char"); +} + +ATF_TC_BODY(invalidchar, tc) +{ + cpuset_t mask; + + ATF_CHECK_EQ(cpuset_parselist("1+3", &mask), CPUSET_PARSE_ERROR); +} + +ATF_TC(all); +ATF_TC_HEAD(all, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test 'all' special cpu-list"); +} + +ATF_TC_BODY(all, tc) +{ + cpuset_t mask; + + ATF_CHECK_EQ(cpuset_parselist("all", &mask), CPUSET_PARSE_OK); +} + +ATF_TC(normalsyntax); +ATF_TC_HEAD(normalsyntax, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test normal cpu-list syntax"); +} + +ATF_TC_BODY(normalsyntax, tc) +{ + cpuset_t mask; + + ATF_CHECK_EQ(cpuset_parselist("1-3,6", &mask), CPUSET_PARSE_OK); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, invalid); + ATF_TP_ADD_TC(tp, invalidchar); + ATF_TP_ADD_TC(tp, all); + ATF_TP_ADD_TC(tp, normalsyntax); + return (atf_no_error()); +} diff --git a/lib/libutil/tests/expand_number_test.c b/lib/libutil/tests/expand_number_test.c new file mode 100644 index 000000000000..9bd339298575 --- /dev/null +++ b/lib/libutil/tests/expand_number_test.c @@ -0,0 +1,294 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 John Baldwin <jhb@FreeBSD.org> + * Copyright (c) 2025 Dag-Erling Smørgrav <des@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 <errno.h> +#include <libutil.h> +#include <stdint.h> + +static void +require_success(const char *str, int64_t exp_val) +{ + int64_t val; + + ATF_REQUIRE_MSG(expand_number(str, &val) == 0, + "Failed to parse '%s': %m", str); + ATF_REQUIRE_MSG(val == exp_val, + "String '%s' parsed as %jd instead of expected %jd", str, + (intmax_t)val, (intmax_t)exp_val); +} + +static void +require_error(const char *str, int exp_errno) +{ + int64_t val; + + ATF_REQUIRE_MSG(expand_number(str, &val) == -1, + "String '%s' parsed as %jd instead of error", str, (intmax_t)val); + ATF_REQUIRE_MSG(errno == exp_errno, + "String '%s' failed with %d instead of expected %d", str, errno, + exp_errno); +} + +ATF_TC_WITHOUT_HEAD(expand_number__ok); +ATF_TC_BODY(expand_number__ok, tp) +{ + /* Bare numbers. */ + require_success("-0", 0); + require_success(" 0", 0); + require_success("+0", 0); + require_success("-1", -1); + require_success(" 1", 1); + require_success("+1", 1); + require_success("-10", -10); + require_success(" 10", 10); + require_success("+10", 10); + + /* Uppercase suffixes. */ + require_success("1B", 1); + require_success("1K", 1LL << 10); + require_success("1M", 1LL << 20); + require_success("1G", 1LL << 30); + require_success("1T", 1LL << 40); + require_success("1P", 1LL << 50); + require_success("1E", 1LL << 60); + + /* Lowercase suffixes. */ + require_success("2b", 2); + require_success("2k", 2LL << 10); + require_success("2m", 2LL << 20); + require_success("2g", 2LL << 30); + require_success("2t", 2LL << 40); + require_success("2p", 2LL << 50); + require_success("2e", 2LL << 60); + + /* Suffixes with a trailing 'b'. */ + require_success("3KB", 3LL << 10); + require_success("3MB", 3LL << 20); + require_success("3GB", 3LL << 30); + require_success("3TB", 3LL << 40); + require_success("3PB", 3LL << 50); + require_success("3EB", 3LL << 60); + + /* Negative numbers. */ + require_success("-1", -1); + require_success("-10", -10); + require_success("-1B", -1); + require_success("-1K", -(1LL << 10)); + require_success("-1M", -(1LL << 20)); + require_success("-1G", -(1LL << 30)); + require_success("-1T", -(1LL << 40)); + require_success("-1P", -(1LL << 50)); + require_success("-1E", -(1LL << 60)); + require_success("-2b", -2); + require_success("-2k", -(2LL << 10)); + require_success("-2m", -(2LL << 20)); + require_success("-2g", -(2LL << 30)); + require_success("-2t", -(2LL << 40)); + require_success("-2p", -(2LL << 50)); + require_success("-2e", -(2LL << 60)); + require_success("-3KB", -(3LL << 10)); + require_success("-3MB", -(3LL << 20)); + require_success("-3GB", -(3LL << 30)); + require_success("-3TB", -(3LL << 40)); + require_success("-3PB", -(3LL << 50)); + require_success("-3EB", -(3LL << 60)); + + /* Maximum values. */ + require_success("7E", 7LL << 60); + require_success("8191P", 8191LL << 50); + require_success("8388607T", 8388607LL << 40); + require_success("8589934591G", 8589934591LL << 30); + require_success("8796093022207M", 8796093022207LL << 20); + require_success("9007199254740991K", 9007199254740991LL << 10); + require_success("9223372036854775807", INT64_MAX); + + /* Minimum values. */ + require_success("-7E", -(7LL << 60)); + require_success("-8191P", -(8191LL << 50)); + require_success("-8388607T", -(8388607LL << 40)); + require_success("-8589934591G", -(8589934591LL << 30)); + require_success("-8796093022207M", -(8796093022207LL << 20)); + require_success("-9007199254740991K", -(9007199254740991LL << 10)); + require_success("-9223372036854775808", INT64_MIN); +} + +ATF_TC_WITHOUT_HEAD(expand_number__bad); +ATF_TC_BODY(expand_number__bad, tp) +{ + /* No digits. */ + require_error("", EINVAL); + require_error("b", EINVAL); + require_error("k", EINVAL); + require_error("m", EINVAL); + require_error("g", EINVAL); + require_error("t", EINVAL); + require_error("p", EINVAL); + require_error("e", EINVAL); + require_error("-", EINVAL); + require_error("-b", EINVAL); + require_error("-k", EINVAL); + require_error("-m", EINVAL); + require_error("-g", EINVAL); + require_error("-t", EINVAL); + require_error("-p", EINVAL); + require_error("-e", EINVAL); + + require_error("not_a_number", EINVAL); + + /* Invalid suffixes. */ + require_error("1a", EINVAL); + require_error("1c", EINVAL); + require_error("1d", EINVAL); + require_error("1f", EINVAL); + require_error("1h", EINVAL); + require_error("1i", EINVAL); + require_error("1j", EINVAL); + require_error("1l", EINVAL); + require_error("1n", EINVAL); + require_error("1o", EINVAL); + require_error("1q", EINVAL); + require_error("1r", EINVAL); + require_error("1s", EINVAL); + require_error("1u", EINVAL); + require_error("1v", EINVAL); + require_error("1w", EINVAL); + require_error("1x", EINVAL); + require_error("1y", EINVAL); + require_error("1z", EINVAL); + + /* Trailing garbage. */ + require_error("1K foo", EINVAL); + require_error("1Mfoo", EINVAL); + + /* Overflow. */ + require_error("8E", ERANGE); + require_error("8192P", ERANGE); + require_error("8388608T", ERANGE); + require_error("8589934592G", ERANGE); + require_error("8796093022208M", ERANGE); + require_error("9007199254740992K", ERANGE); + require_error("9223372036854775808", ERANGE); + + /* Multiple signs */ + require_error("--1", EINVAL); + require_error("-+1", EINVAL); + require_error("+-1", EINVAL); + require_error("++1", EINVAL); + + /* Whitespace after the sign */ + require_error(" - 1", EINVAL); + require_error(" + 1", EINVAL); +} + +ATF_TC_WITHOUT_HEAD(expand_unsigned); +ATF_TC_BODY(expand_unsigned, tp) +{ + static struct tc { + const char *str; + uint64_t num; + int error; + } tcs[] = { + { "0", 0, 0 }, + { "+0", 0, 0 }, + { "-0", 0, 0 }, + { "1", 1, 0 }, + { "+1", 1, 0 }, + { "-1", 0, ERANGE }, + { "18446744073709551615", UINT64_MAX, 0 }, + { "+18446744073709551615", UINT64_MAX, 0 }, + { "-18446744073709551615", 0, ERANGE }, + { 0 }, + }; + struct tc *tc; + uint64_t num; + int error, ret; + + for (tc = tcs; tc->str != NULL; tc++) { + ret = expand_number(tc->str, &num); + error = errno; + if (tc->error == 0) { + ATF_REQUIRE_EQ_MSG(0, ret, + "%s ret = %d", tc->str, ret); + ATF_REQUIRE_EQ_MSG(tc->num, num, + "%s num = %ju", tc->str, (uintmax_t)num); + } else { + ATF_REQUIRE_EQ_MSG(-1, ret, + "%s ret = %d", tc->str, ret); + ATF_REQUIRE_EQ_MSG(tc->error, error, + "%s errno = %d", tc->str, error); + } + } +} + +ATF_TC_WITHOUT_HEAD(expand_generic); +ATF_TC_BODY(expand_generic, tp) +{ + uint64_t uint64; + int64_t int64; +#ifdef __LP64__ + size_t size; +#endif + off_t off; + + ATF_REQUIRE_EQ(0, expand_number("18446744073709551615", &uint64)); + ATF_REQUIRE_EQ(UINT64_MAX, uint64); + ATF_REQUIRE_EQ(-1, expand_number("-1", &uint64)); + ATF_REQUIRE_EQ(ERANGE, errno); + + ATF_REQUIRE_EQ(0, expand_number("9223372036854775807", &int64)); + ATF_REQUIRE_EQ(INT64_MAX, int64); + ATF_REQUIRE_EQ(-1, expand_number("9223372036854775808", &int64)); + ATF_REQUIRE_EQ(ERANGE, errno); + ATF_REQUIRE_EQ(0, expand_number("-9223372036854775808", &int64)); + ATF_REQUIRE_EQ(INT64_MIN, int64); + +#ifdef __LP64__ + ATF_REQUIRE_EQ(0, expand_number("18446744073709551615", &size)); + ATF_REQUIRE_EQ(UINT64_MAX, size); + ATF_REQUIRE_EQ(-1, expand_number("-1", &size)); + ATF_REQUIRE_EQ(ERANGE, errno); +#endif + + ATF_REQUIRE_EQ(0, expand_number("9223372036854775807", &off)); + ATF_REQUIRE_EQ(INT64_MAX, off); + ATF_REQUIRE_EQ(-1, expand_number("9223372036854775808", &off)); + ATF_REQUIRE_EQ(ERANGE, errno); + ATF_REQUIRE_EQ(0, expand_number("-9223372036854775808", &off)); + ATF_REQUIRE_EQ(INT64_MIN, off); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, expand_number__ok); + ATF_TP_ADD_TC(tp, expand_number__bad); + ATF_TP_ADD_TC(tp, expand_unsigned); + ATF_TP_ADD_TC(tp, expand_generic); + + return (atf_no_error()); +} diff --git a/lib/libutil/tests/flopen_test.c b/lib/libutil/tests/flopen_test.c new file mode 100644 index 000000000000..bb810b27f3cc --- /dev/null +++ b/lib/libutil/tests/flopen_test.c @@ -0,0 +1,207 @@ +/*- + * Copyright (c) 2007-2009 Dag-Erling Smørgrav + * 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 + * in this position and unchanged. + * 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/fcntl.h> + +#include <errno.h> +#include <signal.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <libutil.h> + +/* + * Test that flopen() can create a file. + */ +const char * +test_flopen_create(void) +{ + const char *fn = "test_flopen_create"; + const char *result = NULL; + int fd; + + unlink(fn); + fd = flopen(fn, O_RDWR|O_CREAT, 0640); + if (fd < 0) { + result = strerror(errno); + } else { + close(fd); + } + unlink(fn); + return (result); +} + +/* + * Test that flopen() can open an existing file. + */ +const char * +test_flopen_open(void) +{ + const char *fn = "test_flopen_open"; + const char *result = NULL; + int fd; + + fd = open(fn, O_RDWR|O_CREAT, 0640); + if (fd < 0) { + result = strerror(errno); + } else { + close(fd); + fd = flopen(fn, O_RDWR); + if (fd < 0) { + result = strerror(errno); + } else { + close(fd); + } + } + unlink(fn); + return (result); +} + +/* + * Test that flopen() can lock against itself + */ +const char * +test_flopen_lock_self(void) +{ + const char *fn = "test_flopen_lock_self"; + const char *result = NULL; + int fd1, fd2; + + unlink(fn); + fd1 = flopen(fn, O_RDWR|O_CREAT, 0640); + if (fd1 < 0) { + result = strerror(errno); + } else { + fd2 = flopen(fn, O_RDWR|O_NONBLOCK); + if (fd2 >= 0) { + result = "second open succeeded"; + close(fd2); + } + close(fd1); + } + unlink(fn); + return (result); +} + +/* + * Test that flopen() can lock against other processes + */ +const char * +test_flopen_lock_other(void) +{ + const char *fn = "test_flopen_lock_other"; + const char *result = NULL; + volatile int fd1, fd2; + + unlink(fn); + fd1 = flopen(fn, O_RDWR|O_CREAT, 0640); + if (fd1 < 0) { + result = strerror(errno); + } else { + fd2 = -42; + if (vfork() == 0) { + fd2 = flopen(fn, O_RDWR|O_NONBLOCK); + close(fd2); + _exit(0); + } + if (fd2 == -42) + result = "vfork() doesn't work as expected"; + if (fd2 >= 0) + result = "second open succeeded"; + close(fd1); + } + unlink(fn); + return (result); +} + +/* + * Test that child processes inherit the lock + */ +const char * +test_flopen_lock_child(void) +{ + const char *fn = "test_flopen_lock_child"; + const char *result = NULL; + pid_t pid; + volatile int fd1, fd2; + + unlink(fn); + fd1 = flopen(fn, O_RDWR|O_CREAT, 0640); + if (fd1 < 0) { + result = strerror(errno); + } else { + pid = fork(); + if (pid == -1) { + result = strerror(errno); + } else if (pid == 0) { + select(0, 0, 0, 0, 0); + _exit(0); + } + close(fd1); + if ((fd2 = flopen(fn, O_RDWR|O_NONBLOCK)) != -1) { + result = "second open succeeded"; + close(fd2); + } + kill(pid, SIGINT); + } + unlink(fn); + return (result); +} + +static struct test { + const char *name; + const char *(*func)(void); +} t[] = { + { "flopen_create", test_flopen_create }, + { "flopen_open", test_flopen_open }, + { "flopen_lock_self", test_flopen_lock_self }, + { "flopen_lock_other", test_flopen_lock_other }, + { "flopen_lock_child", test_flopen_lock_child }, +}; + +int +main(void) +{ + const char *result; + int i, nt; + + nt = sizeof(t) / sizeof(*t); + printf("1..%d\n", nt); + for (i = 0; i < nt; ++i) { + if ((result = t[i].func()) != NULL) + printf("not ok %d - %s # %s\n", i + 1, + t[i].name, result); + else + printf("ok %d - %s\n", i + 1, + t[i].name); + } + exit(0); +} diff --git a/lib/libutil/tests/forkpty_test.c b/lib/libutil/tests/forkpty_test.c new file mode 100644 index 000000000000..3e54cf310150 --- /dev/null +++ b/lib/libutil/tests/forkpty_test.c @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2023 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/resource.h> +#include <sys/wait.h> + +#include <errno.h> +#include <libutil.h> +#include <unistd.h> + +#include <atf-c.h> + +ATF_TC(forkfail); +ATF_TC_HEAD(forkfail, tc) +{ + atf_tc_set_md_var(tc, "descr", "Check for fd leak when fork() fails"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} + +ATF_TC_BODY(forkfail, tc) +{ + struct rlimit orl, nrl; + pid_t pid; + int prevfd, fd, pty; + + /* set process limit to 1 so fork() will fail */ + ATF_REQUIRE_EQ(0, getrlimit(RLIMIT_NPROC, &orl)); + nrl = orl; + nrl.rlim_cur = 1; + ATF_REQUIRE_EQ(0, setrlimit(RLIMIT_NPROC, &nrl)); + /* check first free fd */ + ATF_REQUIRE((fd = dup(0)) > 0); + ATF_REQUIRE_EQ(0, close(fd)); + /* attempt forkpty() */ + pid = forkpty(&pty, NULL, NULL, NULL); + if (pid == 0) { + /* child - fork() unexpectedly succeeded */ + _exit(0); + } + ATF_CHECK_ERRNO(EAGAIN, pid < 0); + if (pid > 0) { + /* parent - fork() unexpectedly succeeded */ + (void)waitpid(pid, NULL, 0); + } + /* check that first free fd hasn't changed */ + prevfd = fd; + ATF_REQUIRE((fd = dup(0)) > 0); + ATF_CHECK_EQ(prevfd, fd); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, forkfail); + return (atf_no_error()); +} diff --git a/lib/libutil/tests/grp_test.c b/lib/libutil/tests/grp_test.c new file mode 100644 index 000000000000..291e1feabaf0 --- /dev/null +++ b/lib/libutil/tests/grp_test.c @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 2008 Sean C. Farley <scf@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, + * without modification, immediately at the beginning of the file. + * 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 ``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 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 <errno.h> +#include <grp.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <libutil.h> + + +/* + * Static values for building and testing an artificial group. + */ +static char grpName[] = "groupName"; +static char grpPasswd[] = "groupPwd"; +static gid_t grpGID = 1234; +static char *grpMems[] = { "mem1", "mem2", "mem3", NULL }; +static const char *origStrGrp = "groupName:groupPwd:1234:mem1,mem2,mem3"; + + +/* + * Build a group to test against without depending on a real group to be found + * within /etc/group. + */ +static void +build_grp(struct group *grp) +{ + grp->gr_name = grpName; + grp->gr_passwd = grpPasswd; + grp->gr_gid = grpGID; + grp->gr_mem = grpMems; + + return; +} + + +int +main(void) +{ + char *strGrp; + int testNdx; + struct group *dupGrp; + struct group *scanGrp; + struct group origGrp; + + /* Setup. */ + printf("1..4\n"); + testNdx = 0; + + /* Manually build a group using static values. */ + build_grp(&origGrp); + + /* Copy the group. */ + testNdx++; + if ((dupGrp = gr_dup(&origGrp)) == NULL) + printf("not "); + printf("ok %d - %s\n", testNdx, "gr_dup"); + + /* Compare the original and duplicate groups. */ + testNdx++; + if (! gr_equal(&origGrp, dupGrp)) + printf("not "); + printf("ok %d - %s\n", testNdx, "gr_equal"); + + /* Create group string from the duplicate group structure. */ + testNdx++; + strGrp = gr_make(dupGrp); + if (strcmp(strGrp, origStrGrp) != 0) + printf("not "); + printf("ok %d - %s\n", testNdx, "gr_make"); + + /* + * Create group structure from string and compare it to the original + * group structure. + */ + testNdx++; + if ((scanGrp = gr_scan(strGrp)) == NULL || ! gr_equal(&origGrp, + scanGrp)) + printf("not "); + printf("ok %d - %s\n", testNdx, "gr_scan"); + + /* Clean up. */ + free(scanGrp); + free(strGrp); + free(dupGrp); + + exit(EXIT_SUCCESS); +} diff --git a/lib/libutil/tests/humanize_number_test.c b/lib/libutil/tests/humanize_number_test.c new file mode 100644 index 000000000000..3cf427cacc8a --- /dev/null +++ b/lib/libutil/tests/humanize_number_test.c @@ -0,0 +1,592 @@ +/*- + * Copyright 2012 Clifton Royston + * Copyright 2013 John-Mark Gurney + * 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 <inttypes.h> +#include <libutil.h> +#include <limits.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define MAX_STR_FLAGS_RESULT 80 +#define MAX_INT_STR_DIGITS 12 + +static const int64_t halfExabyte = (int64_t)500*1000*1000*1000*1000*1000L; + +static struct { + int retval; + const char *res; + int64_t num; + int flags; + int scale; + size_t buflen; +} test_args[] = { + /* tests 0-13 test 1000 suffixes */ + { 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + + /* tests 14-27 test 1024 suffixes */ + { 2, "0 ", (int64_t)0L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 G", (int64_t)512*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 T", (int64_t)512*1024*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 2, "1 ", (int64_t)1L, 0, HN_AUTOSCALE, 4 }, + { 3, "2 K", (int64_t)1536L, 0, HN_AUTOSCALE, 4 }, + { 3, "2 M", (int64_t)1536*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "2 G", (int64_t)1536*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 4 }, + + /* tests 28-37 test rounding */ + { 3, "0 M", (int64_t)500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "1 M", (int64_t)1000*1000L + 500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "2 M", (int64_t)1000*1000L + 500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 4 }, + { 3, "0 K", (int64_t)512L-1, 0, HN_AUTOSCALE, 4 }, + { 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE, 4 }, + { 3, "0 M", (int64_t)512*1024L-1, 0, HN_AUTOSCALE, 4 }, + { 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE, 4 }, + { 3, "1 M", (int64_t)1024*1024L + 512*1024L-1, 0, HN_AUTOSCALE, 4 }, + { 3, "2 M", (int64_t)1024*1024L + 512*1024L, 0, HN_AUTOSCALE, 4 }, + + /* tests 38-61 test specific scale factors with 1000 divisor */ + { 3, "0 k", (int64_t)0L, HN_DIVISOR_1000, 1, 4 }, + { 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, 1, 4 }, + { 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, 2, 4 }, + { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, 2, 4 }, + { 3, "0 G", (int64_t)500*1000L, HN_DIVISOR_1000, 3, 4 }, + { 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 3, 4 }, + { 3, "0 T", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 4, 4 }, + { 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 4, 4 }, + { 3, "0 P", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 5, 4 }, + { 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5, 4 }, + { 3, "0 E", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 4 }, + { 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 4 }, + { 3, "0 k", (int64_t)1L, HN_DIVISOR_1000, 1, 4 }, + { 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, 1, 4 }, + { 3, "0 M", (int64_t)1500L, HN_DIVISOR_1000, 2, 4 }, + { 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, 2, 4 }, + { 3, "0 G", (int64_t)1500*1000L, HN_DIVISOR_1000, 3, 4 }, + { 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 3, 4 }, + { 3, "0 T", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 4, 4 }, + { 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 4, 4 }, + { 3, "0 P", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 5, 4 }, + { 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5, 4 }, + { 3, "0 E", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 4 }, + { 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 4 }, + + /* tests 62-85 test specific scale factors with 1024 divisor */ + { 3, "0 K", (int64_t)0L, 0, 1, 4 }, + { 3, "1 K", (int64_t)512L, 0, 1, 4 }, + { 3, "0 M", (int64_t)512L, 0, 2, 4 }, + { 3, "1 M", (int64_t)512*1024L, 0, 2, 4 }, + { 3, "0 G", (int64_t)512*1024L, 0, 3, 4 }, + { 3, "1 G", (int64_t)512*1024*1024L, 0, 3, 4 }, + { 3, "0 T", (int64_t)512*1024*1024L, 0, 4, 4 }, + { 3, "1 T", (int64_t)512*1024*1024*1024L, 0, 4, 4 }, + { 3, "0 P", (int64_t)512*1024*1024*1024L, 0, 5, 4 }, + { 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, 5, 4 }, + { 3, "0 E", (int64_t)512*1024*1024*1024*1024L, 0, 6, 4 }, + { 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, 6, 4 }, + { 3, "0 K", (int64_t)1L, 0, 1, 4 }, + { 3, "2 K", (int64_t)1536L, 0, 1, 4 }, + { 3, "0 M", (int64_t)1536L, 0, 2, 4 }, + { 3, "2 M", (int64_t)1536*1024L, 0, 2, 4 }, + { 3, "0 G", (int64_t)1536*1024L, 0, 3, 4 }, + { 3, "2 G", (int64_t)1536*1024*1024L, 0, 3, 4 }, + { 3, "0 T", (int64_t)1536*1024*1024L, 0, 4, 4 }, + { 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, 4, 4 }, + { 3, "0 P", (int64_t)1536*1024*1024*1024L, 0, 5, 4 }, + { 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, 5, 4 }, + { 3, "0 E", (int64_t)1536*1024*1024*1024*1024L, 0, 6, 4 }, + { 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, 6, 4 }, + + /* tests 86-99 test invalid specific scale values of < 0 or >= 7 with + and without HN_DIVISOR_1000 set */ + /* all should return errors with new code; with old, the latter 3 + are instead processed as if having AUTOSCALE and/or GETSCALE set */ + { -1, "", (int64_t)1L, 0, 7, 4 }, + { -1, "", (int64_t)1L, HN_DIVISOR_1000, 7, 4 }, + { -1, "", (int64_t)1L, 0, 1000, 4 }, + { -1, "", (int64_t)1L, HN_DIVISOR_1000, 1000, 4 }, + { -1, "", (int64_t)0L, 0, 1000*1000, 4 }, + { -1, "", (int64_t)0L, HN_DIVISOR_1000, 1000*1000, 4 }, + { -1, "", (int64_t)0L, 0, INT_MAX, 4 }, + { -1, "", (int64_t)0L, HN_DIVISOR_1000, INT_MAX, 4 }, + + /* Negative scale values are not handled well + by the existing library routine - should report as error */ + /* all should return errors with new code, fail assertion with old */ + + { -1, "", (int64_t)1L, 0, -1, 4 }, + { -1, "", (int64_t)1L, HN_DIVISOR_1000, -1, 4 }, + { -1, "", (int64_t)1L, 0, -1000, 4 }, + { -1, "", (int64_t)1L, HN_DIVISOR_1000, -1000, 4 }, + + /* __INT_MIN doesn't print properly, skipped. */ + + { -1, "", (int64_t)1L, 0, -__INT_MAX, 4 }, + { -1, "", (int64_t)1L, HN_DIVISOR_1000, -__INT_MAX, 4 }, + + + /* tests for scale == 0, without autoscale */ + /* tests 100-114 test scale 0 with 1000 divisor - print first N digits */ + { 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, 0, 4 }, + { 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, 0, 4 }, + { 3, "10 ", (int64_t)10L, HN_DIVISOR_1000, 0, 4 }, + { 3, "0 M", (int64_t)150L, HN_DIVISOR_1000, HN_NOSPACE, 4 }, + { 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, HN_NOSPACE, 4 }, + { 3, "0 M", (int64_t)999L, HN_DIVISOR_1000, HN_NOSPACE, 4 }, + { 4, "150", (int64_t)150L, HN_DIVISOR_1000, 0, 4 }, + { 4, "500", (int64_t)500L, HN_DIVISOR_1000, 0, 4 }, + { 4, "999", (int64_t)999L, HN_DIVISOR_1000, 0, 4 }, + { 5, "100", (int64_t)1000L, HN_DIVISOR_1000, 0, 4 }, + { 5, "150", (int64_t)1500L, HN_DIVISOR_1000, 0, 4 }, + { 7, "500", (int64_t)500*1000L, HN_DIVISOR_1000, 0, 4 }, + { 8, "150", (int64_t)1500*1000L, HN_DIVISOR_1000, 0, 4 }, + { 10, "500", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 0, 4 }, + { 11, "150", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 0, 4 }, + + /* tests 115-126 test scale 0 with 1024 divisor - print first N digits */ + { 2, "0 ", (int64_t)0L, 0, 0, 4 }, + { 2, "1 ", (int64_t)1L, 0, 0, 4 }, + { 3, "10 ", (int64_t)10L, 0, 0, 4 }, + { 4, "150", (int64_t)150L, 0, 0, 4 }, + { 4, "500", (int64_t)500L, 0, 0, 4 }, + { 4, "999", (int64_t)999L, 0, 0, 4 }, + { 5, "100", (int64_t)1000L, 0, 0, 4 }, + { 5, "150", (int64_t)1500L, 0, 0, 4 }, + { 7, "500", (int64_t)500*1000L, 0, 0, 4 }, + { 8, "150", (int64_t)1500*1000L, 0, 0, 4 }, + { 10, "500", (int64_t)500*1000*1000L, 0, 0, 4 }, + { 11, "150", (int64_t)1500*1000*1000L, 0, 0, 4 }, + + /* Test case for rounding of edge numbers around 999.5+, see PR224498. + * Require buflen >= 5 */ + { 4, "1.0M", (int64_t)1023500, HN_DECIMAL|HN_B|HN_NOSPACE, HN_AUTOSCALE, 5 }, + + /* Test boundary cases for very large positive/negative number formatting */ + /* Explicit scale, divisor 1024 */ + + /* Requires buflen >= 6 */ + { 3, "8 E", INT64_MAX, 0, 6, 6 }, + { 4, "-8 E", -INT64_MAX, 0, 6, 6 }, + { 3, "0 E", (int64_t)92*1024*1024*1024*1024*1024L, 0, 6, 6 }, + { 3, "0 E", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 6, 6 }, + { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, 0, 6, 6 }, + { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 6, 6 }, + { 3, "0 E", (int64_t)81*1024*1024*1024*1024*1024L, 0, 6, 6 }, + { 3, "0 E", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 6, 6 }, + { 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, 5, 6 }, + { 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 5, 6 }, + { 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, 5, 6 }, + { 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 5, 6 }, + { 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, 5, 6 }, + { 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 5, 6 }, + + /* Explicit scale, divisor 1000 */ + { 3, "9 E", INT64_MAX, HN_DIVISOR_1000, 6, 6 }, + { 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000, 6, 6 }, + { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6, 6 }, + { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6, 6 }, + { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6, 6 }, + { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 6, 6 }, + { 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5, 6 }, + { 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5, 6 }, + { 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5, 6 }, + { 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 5, 6 }, + + /* Autoscale, divisor 1024 */ + { 3, "8 E", INT64_MAX, 0, HN_AUTOSCALE, 6 }, + { 4, "-8 E", -INT64_MAX, 0, HN_AUTOSCALE, 6 }, + { 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 6 }, + { 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 6 }, + { 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 6 }, + { 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 6 }, + { 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 6 }, + { 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE, 6 }, + /* Autoscale, divisor 1000 */ + { 3, "9 E", INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + + /* 0 scale, divisor 1024 */ + { 12, "skdj", INT64_MAX, 0, 0, 6 }, + { 21, "-9223", -INT64_MAX, 0, 0, 6 }, + { 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, 0, 0, 6 }, + { 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 0, 6 }, + { 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, 0, 0, 6 }, + { 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 0, 6 }, + { 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, 0, 0, 6 }, + { 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 0, 6 }, + + /* 0 scale, divisor 1000 */ + /* XXX - why does this fail? */ + { -1, "", INT64_MAX, HN_DIVISOR_1000, 0, 6 }, + { 21, "-9223", -INT64_MAX, HN_DIVISOR_1000, 0, 6 }, + { 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0, 6 }, + { 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0, 6 }, + { 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0, 6 }, + { 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0, 6 }, + /* Expected to pass */ + { 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0, 6 }, + { 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, 0, 6 }, + + + + /* Need to implement tests for GETSCALE */ +/* { ?, "", (int64_t)0L, HN_DIVISOR_1000, HN_GETSCALE, 6 }, + ... +*/ + /* Tests for HN_DECIMAL */ + /* Positive, Autoscale */ + { 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "994 k", (int64_t)994*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "995 k", (int64_t)995*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "1.0 M", (int64_t)1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "994 M", (int64_t)994*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "995 M", (int64_t)995*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + + { 5, "500 K", (int64_t)500*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "994 K", (int64_t)994*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "995 K", (int64_t)995*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "999 K", (int64_t)999*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.0 M", (int64_t)1000*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.0 M", (int64_t)1018*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.0 M", (int64_t)1019*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.5 M", (int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.9 M", (int64_t)1996*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "2.0 M", (int64_t)1997*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "2.0 M", (int64_t)2047*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "2.0 M", (int64_t)2048*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "2.0 M", (int64_t)2099*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "2.1 M", (int64_t)2100*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "9.9 M", (int64_t)10188*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + /* XXX - shouldn't the following two be "10. M"? */ + { 4, "10 M", (int64_t)10189*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 4, "10 M", (int64_t)10240*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "500 M", (int64_t)500*1024*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "994 M", (int64_t)994*1024*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "995 M", (int64_t)995*1024*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "999 M", (int64_t)999*1024*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.0 G", (int64_t)1000*1024*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "1.0 G", (int64_t)1023*1024*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + + /* Negative, Autoscale - should pass */ + { 6, "-1.5 ", -(int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 6, "-1.9 ", -(int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 6, "-9.9 ", -(int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + + { 6, "-1.5 ", -(int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 6, "-1.9 ", -(int64_t)1949*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 6, "-9.7 ", -(int64_t)9949*1024L, HN_DECIMAL, HN_AUTOSCALE, 6 }, + + /* Positive/negative, at maximum scale */ + { 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + /* Negatives work with latest rev only: */ + { 6, "-9.2 ", -INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + { 6, "-8.9 ", -(int64_t)8949*1000*1000*1000*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE, 6 }, + + { 5, "8.0 E", INT64_MAX, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 5, "7.9 E", INT64_MAX-(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 6, "-8.0 ", -INT64_MAX, HN_DECIMAL, HN_AUTOSCALE, 6 }, + { 6, "-7.9 ", -INT64_MAX+(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE, 6 }, + + /* Positive, Fixed scales */ + { 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1, 6 }, + { 5, "0.5 M", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "949 k", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1, 6 }, + { 5, "0.9 M", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "950 k", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1, 6 }, + { 5, "1.0 M", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1, 6 }, + { 5, "1.0 M", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "0.5 G", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3, 6 }, + { 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2, 6 }, + { 5, "1.0 G", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3, 6 }, + /* Positive/negative, at maximum scale */ + { 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 5, 6 }, + { 5, "1.0 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 6 }, + { 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 6 }, + { 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6, 6 }, + { 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, 6, 6 }, + + /* HN_DECIMAL + binary + fixed scale cases not completed */ + { 5, "512 K", (int64_t)512*1024L, HN_DECIMAL, 1, 6 }, + { 5, "0.5 M", (int64_t)512*1024L, HN_DECIMAL, 2, 6 }, + + /* Negative, Fixed scales */ + /* Not yet added, but should work with latest rev */ + +}; + + +/* Command line options usage */ +static void +usage(char * progname) { + printf("%s: tests libutil humanize_number function\n", progname); + printf("Usage: %s [-nE] [-l num] [-v]\n\n", progname); + printf("Options:\n"); + printf("\t-l num\tSet max length for result; buflen = num + 1\n"); + printf("\t\t (NOTE: does not change expected result strings.)\n"); + printf("\t-n\tInclude negative scale tests, which cause older libutil\n"); + printf("\t\t version of function to coredump with assertion failure\n"); + printf("\t-E\tInclude numbers > 1/2 Exa[byte] which currently fail\n"); + printf("\t-v\tVerbose - always print summary results\n"); + printf("\t-h, -?\tShow options\n"); +} + +/* Parse command line options */ +static void +read_options(int argc, char * const argv[], size_t *bufLength, + int *includeNegativeScale, int *includeExabytes, int *verbose) { + int ch; + size_t temp; + + while ((ch = getopt(argc, argv, "nEh?vl:")) != -1) { + switch (ch) { + default: + usage(argv[0]); + exit(1); + break; /* UNREACHABLE */ + case 'h' : + case '?' : + usage(argv[0]); + exit(0); + break; /* UNREACHABLE */ + case 'l' : + sscanf(optarg, "%zu", &temp); + *bufLength = temp + 1; + break; + case 'n' : + *includeNegativeScale = 1; + break; + case 'E' : + *includeExabytes = 1; + break; + case 'v' : + *verbose = 1; + break; + } + } +} + +static struct { + int value; + const char *name; + } flags[] = { + { HN_AUTOSCALE, "HN_AUTOSCALE" }, + { HN_GETSCALE, "HN_GETSCALE" }, + { HN_DIVISOR_1000, "HN_DIVISOR_1000" }, + { HN_B, "HN_B" }, + { HN_DECIMAL, "HN_DECIMAL" }, +}; + +static const char *separator = "|"; + +/* Format flags parameter for meaningful display */ +static char * +str_flags(int hn_flags, char *noFlags) { + size_t i; + char * result; + + result = malloc(MAX_STR_FLAGS_RESULT); + result[0] = '\0'; + + for (i = 0; i < sizeof flags / sizeof *flags; i++) { + if (hn_flags & flags[i].value) { + if (*result != 0) + strlcat(result, separator, + MAX_STR_FLAGS_RESULT); + strlcat(result, flags[i].name, MAX_STR_FLAGS_RESULT); + } + } + + if (strlen(result) == 0) + strlcat(result, noFlags, MAX_STR_FLAGS_RESULT); + return result; +} + + +/* Format scale parameter for meaningful display */ +static char * +str_scale(int scale) { + char *result; + + if (scale == HN_AUTOSCALE || scale == HN_GETSCALE) + return str_flags(scale, ""); + + result = malloc(MAX_INT_STR_DIGITS); + result[0] = '\0'; + snprintf(result, MAX_INT_STR_DIGITS, "%d", scale); + return result; +} + +static void +testskipped(size_t i) +{ + + printf("ok %zu # skip - not turned on\n", i); +} + +int +main(int argc, char * const argv[]) +{ + char *buf; + char *flag_str, *scale_str; + size_t blen, buflen, errcnt, i, skipped, tested; + int r; + int includeNegScale; + int includeExabyteTests; + int verbose; + + buf = NULL; + buflen = 0; + includeNegScale = 0; + includeExabyteTests = 0; + verbose = 0; + + read_options(argc, argv, &buflen, &includeNegScale, + &includeExabyteTests, &verbose); + + errcnt = 0; + tested = 0; + skipped = 0; + + if (buflen != 4) + printf("Warning: buffer size %zu != 4, expect some results to differ.\n", buflen); + + printf("1..%zu\n", nitems(test_args)); + for (i = 0; i < nitems(test_args); i++) { + blen = (buflen > 0) ? buflen : test_args[i].buflen; + buf = realloc(buf, blen); + + if (test_args[i].scale < 0 && ! includeNegScale) { + skipped++; + testskipped(i + 1); + continue; + } + if (test_args[i].num >= halfExabyte && ! includeExabyteTests) { + skipped++; + testskipped(i + 1); + continue; + } + + r = humanize_number(buf, blen, test_args[i].num, "", + test_args[i].scale, test_args[i].flags); + flag_str = str_flags(test_args[i].flags, "[no flags]"); + scale_str = str_scale(test_args[i].scale); + + if (r != test_args[i].retval) { + if (verbose) + printf("wrong return value on index %zu, " + "buflen: %zu, got: %d + \"%s\", " + "expected %d + \"%s\"; num = %jd, " + "scale = %s, flags= %s.\n", + i, blen, r, buf, test_args[i].retval, + test_args[i].res, + (intmax_t)test_args[i].num, + scale_str, flag_str); + else + printf("not ok %zu # return %d != %d\n", + i + 1, r, test_args[i].retval); + errcnt++; + } else if (strcmp(buf, test_args[i].res) != 0) { + if (verbose) + printf("result mismatch on index %zu, got: " + "\"%s\", expected \"%s\"; num = %jd, " + "buflen: %zu, scale = %s, flags= %s.\n", + i, buf, test_args[i].res, + (intmax_t)test_args[i].num, + blen, scale_str, flag_str); + else + printf("not ok %zu # buf \"%s\" != \"%s\"\n", + i + 1, buf, test_args[i].res); + errcnt++; + } else { + if (verbose) + printf("successful result on index %zu, " + "returned %d, got: \"%s\"; num = %jd, " + "buflen = %zd, scale = %s, flags= %s.\n", + i, r, buf, (intmax_t)test_args[i].num, + blen, scale_str, flag_str); + else + printf("ok %zu\n", i + 1); + } + tested++; + } + free(buf); + + if (verbose) + printf("total errors: %zu/%zu tests, %zu skipped\n", errcnt, + tested, skipped); + + if (errcnt) + return 1; + + return 0; +} diff --git a/lib/libutil/tests/pidfile_test.c b/lib/libutil/tests/pidfile_test.c new file mode 100644 index 000000000000..602bc6cea983 --- /dev/null +++ b/lib/libutil/tests/pidfile_test.c @@ -0,0 +1,326 @@ +/*- + * Copyright (c) 2007-2009 Dag-Erling Smørgrav + * 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 + * in this position and unchanged. + * 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 <sys/event.h> + +#include <fcntl.h> +#include <errno.h> +#include <signal.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <libutil.h> + +/* + * We need a signal handler so kill(2) will interrupt the child + * instead of killing it. + */ +static void +signal_handler(int sig) +{ + (void)sig; +} + +/* + * Test that pidfile_open() can create a pidfile and that pidfile_write() + * can write to it. + */ +static const char * +test_pidfile_uncontested(void) +{ + const char *fn = "test_pidfile_uncontested"; + struct pidfh *pf; + pid_t other = 0; + + unlink(fn); + pf = pidfile_open(fn, 0600, &other); + if (pf == NULL && other != 0) + return ("pidfile exists and is locked"); + if (pf == NULL) + return (strerror(errno)); + if (pidfile_write(pf) != 0) { + pidfile_close(pf); + unlink(fn); + return ("failed to write PID"); + } + pidfile_close(pf); + unlink(fn); + return (NULL); +} + +/* + * Test that pidfile_open() locks against self. + */ +static const char * +test_pidfile_self(void) +{ + const char *fn = "test_pidfile_self"; + struct pidfh *pf1, *pf2; + pid_t other = 0; + int serrno; + + unlink(fn); + pf1 = pidfile_open(fn, 0600, &other); + if (pf1 == NULL && other != 0) + return ("pidfile exists and is locked"); + if (pf1 == NULL) + return (strerror(errno)); + if (pidfile_write(pf1) != 0) { + serrno = errno; + pidfile_close(pf1); + unlink(fn); + return (strerror(serrno)); + } + // second open should fail + pf2 = pidfile_open(fn, 0600, &other); + if (pf2 != NULL) { + pidfile_close(pf1); + pidfile_close(pf2); + unlink(fn); + return ("managed to opened pidfile twice"); + } + if (other != getpid()) { + pidfile_close(pf1); + unlink(fn); + return ("pidfile contained wrong PID"); + } + pidfile_close(pf1); + unlink(fn); + return (NULL); +} + +/* + * Common code for test_pidfile_{contested,inherited}. + */ +static const char * +common_test_pidfile_child(const char *fn, int parent_open) +{ + struct pidfh *pf = NULL; + pid_t other = 0, pid = 0; + int fd[2], serrno, status; + struct kevent event, ke; + char ch; + int kq; + + unlink(fn); + if (pipe(fd) != 0) + return (strerror(errno)); + + if (parent_open) { + pf = pidfile_open(fn, 0600, &other); + if (pf == NULL && other != 0) + return ("pidfile exists and is locked"); + if (pf == NULL) + return (strerror(errno)); + } + + pid = fork(); + if (pid == -1) + return (strerror(errno)); + if (pid == 0) { + // child + close(fd[0]); + signal(SIGINT, signal_handler); + if (!parent_open) { + pf = pidfile_open(fn, 0600, &other); + if (pf == NULL && other != 0) + return ("pidfile exists and is locked"); + if (pf == NULL) + return (strerror(errno)); + } + if (pidfile_write(pf) != 0) { + serrno = errno; + pidfile_close(pf); + unlink(fn); + return (strerror(serrno)); + } + if (pf == NULL) + _exit(1); + if (pidfile_write(pf) != 0) + _exit(2); + kq = kqueue(); + if (kq == -1) + _exit(3); + EV_SET(&ke, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); + /* Attach event to the kqueue. */ + if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0) + _exit(4); + /* Inform the parent we are ready to receive SIGINT */ + if (write(fd[1], "*", 1) != 1) + _exit(5); + /* Wait for SIGINT received */ + if (kevent(kq, NULL, 0, &event, 1, NULL) != 1) + _exit(6); + _exit(0); + } + // parent + close(fd[1]); + if (pf) + pidfile_close(pf); + + // wait for the child to signal us + if (read(fd[0], &ch, 1) != 1) { + serrno = errno; + unlink(fn); + kill(pid, SIGTERM); + errno = serrno; + return (strerror(errno)); + } + + // We shouldn't be able to lock the same pidfile as our child + pf = pidfile_open(fn, 0600, &other); + if (pf != NULL) { + pidfile_close(pf); + unlink(fn); + return ("managed to lock contested pidfile"); + } + + // Failed to lock, but not because it was contested + if (other == 0) { + unlink(fn); + return (strerror(errno)); + } + + // Locked by the wrong process + if (other != pid) { + unlink(fn); + return ("pidfile contained wrong PID"); + } + + // check our child's fate + if (pf) + pidfile_close(pf); + unlink(fn); + if (kill(pid, SIGINT) != 0) + return (strerror(errno)); + if (waitpid(pid, &status, 0) == -1) + return (strerror(errno)); + if (WIFSIGNALED(status)) + return ("child caught signal"); + if (WEXITSTATUS(status) != 0) + return ("child returned non-zero status"); + + // success + return (NULL); +} + +/* + * Test that pidfile_open() fails when attempting to open a pidfile that + * is already locked, and that it returns the correct PID. + */ +static const char * +test_pidfile_contested(void) +{ + const char *fn = "test_pidfile_contested"; + const char *result; + + result = common_test_pidfile_child(fn, 0); + return (result); +} + +/* + * Test that the pidfile lock is inherited. + */ +static const char * +test_pidfile_inherited(void) +{ + const char *fn = "test_pidfile_inherited"; + const char *result; + + result = common_test_pidfile_child(fn, 1); + return (result); +} + +/* + * Make sure we handle relative pidfile paths correctly. + */ +static const char * +test_pidfile_relative(void) +{ + char path[PATH_MAX], pid[32], tmpdir[PATH_MAX]; + struct pidfh *pfh; + int fd; + + (void)snprintf(tmpdir, sizeof(tmpdir), "%s.XXXXXX", __func__); + if (mkdtemp(tmpdir) == NULL) + return (strerror(errno)); + (void)snprintf(path, sizeof(path), "%s/pidfile", tmpdir); + + pfh = pidfile_open(path, 0600, NULL); + if (pfh == NULL) + return (strerror(errno)); + if (pidfile_write(pfh) != 0) + return (strerror(errno)); + fd = open(path, O_RDONLY); + if (fd < 0) + return (strerror(errno)); + memset(pid, 0, sizeof(pid)); + if (read(fd, pid, sizeof(pid) - 1) < 0) + return (strerror(errno)); + if (atoi(pid) != getpid()) + return ("pid mismatch"); + if (close(fd) != 0) + return (strerror(errno)); + if (pidfile_close(pfh) != 0) + return (strerror(errno)); + return (NULL); +} + +static struct test { + const char *name; + const char *(*func)(void); +} t[] = { + { "pidfile_uncontested", test_pidfile_uncontested }, + { "pidfile_self", test_pidfile_self }, + { "pidfile_contested", test_pidfile_contested }, + { "pidfile_inherited", test_pidfile_inherited }, + { "pidfile_relative", test_pidfile_relative }, +}; + +int +main(void) +{ + const char *result; + int i, nt; + + nt = sizeof(t) / sizeof(*t); + printf("1..%d\n", nt); + for (i = 0; i < nt; ++i) { + if ((result = t[i].func()) != NULL) + printf("not ok %d - %s # %s\n", i + 1, + t[i].name, result); + else + printf("ok %d - %s\n", i + 1, + t[i].name); + } + exit(0); +} diff --git a/lib/libutil/tests/trimdomain-nodomain_test.c b/lib/libutil/tests/trimdomain-nodomain_test.c new file mode 100644 index 000000000000..b2dfaa4ae9af --- /dev/null +++ b/lib/libutil/tests/trimdomain-nodomain_test.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2005 Brooks Davis. 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 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 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/param.h> +#include <errno.h> +#include <libutil.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <ssp/ssp.h> + +#define TESTDOMAIN "" +#define TESTHOST "testhost" +#define TESTFQDN "testhost" TESTDOMAIN + +int failures = 0; +int tests = 0; + +/* + * Evily override gethostname(3) so trimdomain always gets the same result. + * This makes the tests much easier to write and less likely to fail on + * oddly configured systems. + */ +int +__ssp_real(gethostname)(char *name, size_t namelen) +{ + if (strlcpy(name, TESTFQDN, namelen) > namelen) { + errno = ENAMETOOLONG; + return (-1); + } + return (0); +} + +void +testit(const char *input, int hostsize, const char *output, const char *test) +{ + char *testhost; + const char *expected = (output == NULL) ? input : output; + + testhost = strdup(input); + trimdomain(testhost, hostsize < 0 ? (int)strlen(testhost) : hostsize); + tests++; + if (strcmp(testhost, expected) != 0) { + printf("not ok %d - %s\n", tests, test); + printf("# %s -> %s (expected %s)\n", input, testhost, expected); + } else + printf("ok %d - %s\n", tests, test); + free(testhost); + return; +} + +int +main(void) +{ + + printf("1..5\n"); + + testit(TESTFQDN, -1, TESTHOST, "self"); + testit("XXX" TESTDOMAIN, -1, "XXX", "different host, same domain"); + testit("XXX" TESTDOMAIN, 1, NULL, "short hostsize"); + testit("bogus.example.net", -1, NULL, "arbitrary host"); + testit("XXX." TESTFQDN, -1, NULL, "domain is local hostname"); + + return (0); +} diff --git a/lib/libutil/tests/trimdomain_test.c b/lib/libutil/tests/trimdomain_test.c new file mode 100644 index 000000000000..ad5b92b0ce1e --- /dev/null +++ b/lib/libutil/tests/trimdomain_test.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2005 Brooks Davis. 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 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 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/param.h> +#include <errno.h> +#include <libutil.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <ssp/ssp.h> + +#define TESTDOMAIN ".domain.example.com" +#define TESTHOST "testhost" +#define TESTFQDN "testhost" TESTDOMAIN + +int failures = 0; +int tests = 0; + +/* + * Evily override gethostname(3) so trimdomain always gets the same result. + * This makes the tests much easier to write and less likely to fail on + * oddly configured systems. + */ +int +__ssp_real(gethostname)(char *name, size_t namelen) +{ + if (strlcpy(name, TESTFQDN, namelen) > namelen) { + errno = ENAMETOOLONG; + return (-1); + } + return (0); +} + +void +testit(const char *input, int hostsize, const char *output, const char *test) +{ + char *testhost; + const char *expected = (output == NULL) ? input : output; + + testhost = strdup(input); + trimdomain(testhost, hostsize < 0 ? (int)strlen(testhost) : hostsize); + tests++; + if (strcmp(testhost, expected) != 0) { + printf("not ok %d - %s\n", tests, test); + printf("# %s -> %s (expected %s)\n", input, testhost, expected); + } else + printf("ok %d - %s\n", tests, test); + free(testhost); + return; +} + +int +main(void) +{ + + printf("1..5\n"); + + testit(TESTFQDN, -1, TESTHOST, "self"); + testit("XXX" TESTDOMAIN, -1, "XXX", "different host, same domain"); + testit("XXX" TESTDOMAIN, 1, NULL, "short hostsize"); + testit("bogus.example.net", -1, NULL, "arbitrary host"); + testit("XXX." TESTFQDN, -1, NULL, "domain is local hostname"); + + return (0); +} |