diff options
Diffstat (limited to 'lib/libutil/tests')
-rw-r--r-- | lib/libutil/tests/Makefile | 1 | ||||
-rw-r--r-- | lib/libutil/tests/expand_number_test.c | 292 | ||||
-rw-r--r-- | lib/libutil/tests/flopen_test.c | 1 | ||||
-rw-r--r-- | lib/libutil/tests/grp_test.c | 1 | ||||
-rw-r--r-- | lib/libutil/tests/pidfile_test.c | 1 | ||||
-rw-r--r-- | lib/libutil/tests/trimdomain-nodomain_test.c | 5 | ||||
-rw-r--r-- | lib/libutil/tests/trimdomain_test.c | 5 |
7 files changed, 257 insertions, 49 deletions
diff --git a/lib/libutil/tests/Makefile b/lib/libutil/tests/Makefile index d29045d78a10..cdcf3466aec8 100644 --- a/lib/libutil/tests/Makefile +++ b/lib/libutil/tests/Makefile @@ -1,4 +1,3 @@ - TAP_TESTS_C+= flopen_test TAP_TESTS_C+= grp_test TAP_TESTS_C+= humanize_number_test diff --git a/lib/libutil/tests/expand_number_test.c b/lib/libutil/tests/expand_number_test.c index 6bf3ff082ee4..9bd339298575 100644 --- a/lib/libutil/tests/expand_number_test.c +++ b/lib/libutil/tests/expand_number_test.c @@ -1,7 +1,8 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2023 Google LLC + * 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 @@ -25,60 +26,269 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> +#include <atf-c.h> #include <errno.h> #include <libutil.h> +#include <stdint.h> -#include <atf-c.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); +} -ATF_TC_WITHOUT_HEAD(positivetests); -ATF_TC_BODY(positivetests, tc) +static void +require_error(const char *str, int exp_errno) { - int retval; - uint64_t num; + 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); -#define positive_tc(string, value) \ - do { \ - ATF_CHECK_ERRNO(0, (retval = expand_number((string), &num)) == 0); \ - ATF_CHECK_EQ(retval, 0); \ - ATF_CHECK_EQ(num, (value)); \ - } while (0) - - positive_tc("123456", 123456); - positive_tc("123456b", 123456); - positive_tc("1k", 1024); - positive_tc("1kb", 1024); - positive_tc("1K", 1024); - positive_tc("1KB", 1024); - positive_tc("1m", 1048576); - positive_tc("1M", 1048576); - positive_tc("1g", 1073741824); - positive_tc("1G", 1073741824); - positive_tc("1t", 1099511627776); - positive_tc("1T", 1099511627776); - positive_tc("1p", 1125899906842624); - positive_tc("1P", 1125899906842624); - positive_tc("1e", 1152921504606846976); - positive_tc("1E", 1152921504606846976); - positive_tc("15E", 17293822569102704640ULL); + /* Whitespace after the sign */ + require_error(" - 1", EINVAL); + require_error(" + 1", EINVAL); } -ATF_TC_WITHOUT_HEAD(negativetests); -ATF_TC_BODY(negativetests, tc) +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; - ATF_CHECK_ERRNO(EINVAL, expand_number("", &num)); - ATF_CHECK_ERRNO(EINVAL, expand_number("x", &num)); - ATF_CHECK_ERRNO(EINVAL, expand_number("1bb", &num)); - ATF_CHECK_ERRNO(EINVAL, expand_number("1x", &num)); - ATF_CHECK_ERRNO(EINVAL, expand_number("1kx", &num)); - ATF_CHECK_ERRNO(ERANGE, expand_number("16E", &num)); + 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, positivetests); - ATF_TP_ADD_TC(tp, negativetests); + 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 index 94129ebfac46..bb810b27f3cc 100644 --- a/lib/libutil/tests/flopen_test.c +++ b/lib/libutil/tests/flopen_test.c @@ -25,7 +25,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> #include <sys/types.h> #include <sys/fcntl.h> diff --git a/lib/libutil/tests/grp_test.c b/lib/libutil/tests/grp_test.c index 824600905289..291e1feabaf0 100644 --- a/lib/libutil/tests/grp_test.c +++ b/lib/libutil/tests/grp_test.c @@ -24,7 +24,6 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> #include <sys/types.h> #include <errno.h> #include <grp.h> diff --git a/lib/libutil/tests/pidfile_test.c b/lib/libutil/tests/pidfile_test.c index bb11d6a19b79..602bc6cea983 100644 --- a/lib/libutil/tests/pidfile_test.c +++ b/lib/libutil/tests/pidfile_test.c @@ -25,7 +25,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> #include <sys/param.h> #include <sys/wait.h> #include <sys/event.h> diff --git a/lib/libutil/tests/trimdomain-nodomain_test.c b/lib/libutil/tests/trimdomain-nodomain_test.c index ecbfe96e8c3a..b2dfaa4ae9af 100644 --- a/lib/libutil/tests/trimdomain-nodomain_test.c +++ b/lib/libutil/tests/trimdomain-nodomain_test.c @@ -23,7 +23,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> #include <sys/types.h> #include <sys/param.h> #include <errno.h> @@ -33,6 +32,8 @@ #include <string.h> #include <unistd.h> +#include <ssp/ssp.h> + #define TESTDOMAIN "" #define TESTHOST "testhost" #define TESTFQDN "testhost" TESTDOMAIN @@ -46,7 +47,7 @@ int tests = 0; * oddly configured systems. */ int -gethostname(char *name, size_t namelen) +__ssp_real(gethostname)(char *name, size_t namelen) { if (strlcpy(name, TESTFQDN, namelen) > namelen) { errno = ENAMETOOLONG; diff --git a/lib/libutil/tests/trimdomain_test.c b/lib/libutil/tests/trimdomain_test.c index 6ae55d7e8756..ad5b92b0ce1e 100644 --- a/lib/libutil/tests/trimdomain_test.c +++ b/lib/libutil/tests/trimdomain_test.c @@ -23,7 +23,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> #include <sys/types.h> #include <sys/param.h> #include <errno.h> @@ -33,6 +32,8 @@ #include <string.h> #include <unistd.h> +#include <ssp/ssp.h> + #define TESTDOMAIN ".domain.example.com" #define TESTHOST "testhost" #define TESTFQDN "testhost" TESTDOMAIN @@ -46,7 +47,7 @@ int tests = 0; * oddly configured systems. */ int -gethostname(char *name, size_t namelen) +__ssp_real(gethostname)(char *name, size_t namelen) { if (strlcpy(name, TESTFQDN, namelen) > namelen) { errno = ENAMETOOLONG; |