diff options
Diffstat (limited to 'src/test')
54 files changed, 390 insertions, 391 deletions
diff --git a/src/test/meson.build b/src/test/meson.build index 2a4dfe26db..9d193651ed 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -41,6 +41,8 @@ test_dlopen_c = files('test-dlopen.c') ############################################################ tests += [ + [files('test-argv-util.c')], + [files('test-device-nodes.c')], [files('test-ether-addr-util.c')], @@ -205,7 +207,11 @@ tests += [ [files('test-ratelimit.c')], - [files('test-util.c')], + [files('test-raw-clone.c')], + + [files('test-limits-util.c')], + + [files('test-logarithm.c')], [files('test-macro.c')], @@ -213,6 +219,8 @@ tests += [ [], [libm]], + [files('test-memory-util.c')], + [files('test-mkdir.c')], [files('test-json.c'), diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c index 644bc9e27b..45655d792e 100644 --- a/src/test/test-af-list.c +++ b/src/test/test-af-list.c @@ -5,7 +5,6 @@ #include "macro.h" #include "string-util.h" #include "tests.h" -#include "util.h" _unused_ static const struct af_name* lookup_af(register const char *str, register GPERF_LEN_TYPE len); diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c index b542fe1ddf..043978e9a6 100644 --- a/src/test/test-architecture.c +++ b/src/test/test-architecture.c @@ -4,7 +4,6 @@ #include "errno-util.h" #include "log.h" #include "tests.h" -#include "util.h" #include "virt.h" int main(int argc, char *argv[]) { diff --git a/src/test/test-argv-util.c b/src/test/test-argv-util.c new file mode 100644 index 0000000000..26f6038534 --- /dev/null +++ b/src/test/test-argv-util.c @@ -0,0 +1,123 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include <sched.h> +#include <unistd.h> + +#if HAVE_VALGRIND_VALGRIND_H +# include <valgrind/valgrind.h> +#endif + +#include "argv-util.h" +#include "missing_sched.h" +#include "process-util.h" +#include "tests.h" +#include "virt.h" + +static void test_rename_process_now(const char *p, int ret) { + _cleanup_free_ char *comm = NULL, *cmdline = NULL; + int r; + + log_info("/* %s(%s) */", __func__, p); + + r = rename_process(p); + assert_se(r == ret || + (ret == 0 && r >= 0) || + (ret > 0 && r > 0)); + + log_debug_errno(r, "rename_process(%s): %m", p); + + if (r < 0) + return; + +#if HAVE_VALGRIND_VALGRIND_H + /* see above, valgrind is weird, we can't verify what we are doing here */ + if (RUNNING_ON_VALGRIND) + return; +#endif + + assert_se(get_process_comm(0, &comm) >= 0); + log_debug("comm = <%s>", comm); + assert_se(strneq(comm, p, TASK_COMM_LEN-1)); + /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the + * future. We'd only check the initial part, at least until we recompile, but this will still pass. */ + + r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline); + assert_se(r >= 0); + /* we cannot expect cmdline to be renamed properly without privileges */ + if (geteuid() == 0) { + if (r == 0 && detect_container() > 0) + log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline); + else { + log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p); + + bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */ + + assert_se(strneq(cmdline + skip, p, strlen("test-process-util"))); + assert_se(startswith(cmdline + skip, p)); + } + } else + log_info("cmdline = <%s> (not verified)", cmdline); +} + +static void test_rename_process_one(const char *p, int ret) { + siginfo_t si; + pid_t pid; + + log_info("/* %s(%s) */", __func__, p); + + pid = fork(); + assert_se(pid >= 0); + + if (pid == 0) { + /* child */ + test_rename_process_now(p, ret); + _exit(EXIT_SUCCESS); + } + + assert_se(wait_for_terminate(pid, &si) >= 0); + assert_se(si.si_code == CLD_EXITED); + assert_se(si.si_status == EXIT_SUCCESS); +} + +TEST(rename_process_invalid) { + assert_se(rename_process(NULL) == -EINVAL); + assert_se(rename_process("") == -EINVAL); +} + +TEST(rename_process_multi) { + pid_t pid; + + pid = fork(); + assert_se(pid >= 0); + + if (pid > 0) { + siginfo_t si; + + assert_se(wait_for_terminate(pid, &si) >= 0); + assert_se(si.si_code == CLD_EXITED); + assert_se(si.si_status == EXIT_SUCCESS); + + return; + } + + /* child */ + test_rename_process_now("one", 1); + test_rename_process_now("more", 0); /* longer than "one", hence truncated */ + (void) setresuid(99, 99, 99); /* change uid when running privileged */ + test_rename_process_now("time!", 0); + test_rename_process_now("0", 1); /* shorter than "one", should fit */ + _exit(EXIT_SUCCESS); +} + +TEST(rename_process) { + test_rename_process_one("foo", 1); /* should always fit */ + test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ + test_rename_process_one("1234567", 1); /* should always fit */ +} + +static int intro(void) { + log_show_color(true); + return EXIT_SUCCESS; +} + +DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); diff --git a/src/test/test-async.c b/src/test/test-async.c index 8eefad5044..b97fedcfc6 100644 --- a/src/test/test-async.c +++ b/src/test/test-async.c @@ -6,7 +6,6 @@ #include "async.h" #include "macro.h" #include "tmpfile-util.h" -#include "util.h" static bool test_async = false; diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c index 50ceb3a355..b255dba068 100644 --- a/src/test/test-barrier.c +++ b/src/test/test-barrier.c @@ -18,7 +18,6 @@ #include "errno-util.h" #include "tests.h" #include "time-util.h" -#include "util.h" #include "virt.h" /* 20ms to test deadlocks; All timings use multiples of this constant as diff --git a/src/test/test-boot-timestamps.c b/src/test/test-boot-timestamps.c index a9875defdd..53e378eaab 100644 --- a/src/test/test-boot-timestamps.c +++ b/src/test/test-boot-timestamps.c @@ -9,7 +9,6 @@ #include "errno-util.h" #include "log.h" #include "tests.h" -#include "util.h" static int test_acpi_fpdt(void) { usec_t loader_start, loader_exit; diff --git a/src/test/test-btrfs.c b/src/test/test-btrfs.c index 77ec801903..67acba23a7 100644 --- a/src/test/test-btrfs.c +++ b/src/test/test-btrfs.c @@ -8,7 +8,6 @@ #include "format-util.h" #include "log.h" #include "string-util.h" -#include "util.h" int main(int argc, char *argv[]) { BtrfsQuotaInfo quota; diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index 38f7420324..68f0461a62 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -9,7 +9,6 @@ #include "parse-util.h" #include "string-util.h" #include "tests.h" -#include "util.h" /* verify the capability parser */ TEST(cap_list) { diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 7113b07a95..0b286ed8e4 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -15,7 +15,6 @@ #include "strv.h" #include "tests.h" #include "user-util.h" -#include "util.h" #include "version.h" static void check_p_d_u(const char *path, int code, const char *result) { diff --git a/src/test/test-conf-files.c b/src/test/test-conf-files.c index da8f3a97f2..beda749fb8 100644 --- a/src/test/test-conf-files.c +++ b/src/test/test-conf-files.c @@ -19,7 +19,6 @@ #include "strv.h" #include "tests.h" #include "user-util.h" -#include "util.h" static void setup_test_dir(char *tmp_dir, const char *files, ...) { va_list ap; diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c index 8c27dcac3f..0acb4131b5 100644 --- a/src/test/test-conf-parser.c +++ b/src/test/test-conf-parser.c @@ -9,7 +9,6 @@ #include "strv.h" #include "tests.h" #include "tmpfile-util.h" -#include "util.h" static void test_config_parse_path_one(const char *rvalue, const char *expected) { _cleanup_free_ char *path = NULL; diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 4091b425d1..e4e38ac731 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -20,7 +20,6 @@ #include "tests.h" #include "tmpfile-util.h" #include "user-util.h" -#include "util.h" #include "xattr-util.h" TEST(copy_file) { diff --git a/src/test/test-ellipsize.c b/src/test/test-ellipsize.c index 7317193363..2b27b44efb 100644 --- a/src/test/test-ellipsize.c +++ b/src/test/test-ellipsize.c @@ -3,12 +3,11 @@ #include <stdio.h> #include "alloc-util.h" -#include "def.h" +#include "constants.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" #include "tests.h" -#include "util.h" #include "utf8.h" static void test_ellipsize_mem_one(const char *s, size_t old_length, size_t new_length) { diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c index cc37d96327..4796e3c208 100644 --- a/src/test/test-env-util.c +++ b/src/test/test-env-util.c @@ -10,7 +10,6 @@ #include "string-util.h" #include "strv.h" #include "tests.h" -#include "util.h" TEST(strv_env_delete) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; diff --git a/src/test/test-errno-list.c b/src/test/test-errno-list.c index 6c8f384cab..f91a1f770f 100644 --- a/src/test/test-errno-list.c +++ b/src/test/test-errno-list.c @@ -7,7 +7,6 @@ #include "macro.h" #include "string-util.h" #include "tests.h" -#include "util.h" TEST(errno_list) { for (size_t i = 0; i < ELEMENTSOF(errno_names); i++) { diff --git a/src/test/test-errno-util.c b/src/test/test-errno-util.c index f858927c92..d3d022c33f 100644 --- a/src/test/test-errno-util.c +++ b/src/test/test-errno-util.c @@ -47,4 +47,35 @@ TEST(STRERROR_OR_ELSE) { log_info("STRERROR_OR_ELSE(-EPERM, \"EOF\") → %s", STRERROR_OR_EOF(-EPERM)); } +TEST(PROTECT_ERRNO) { + errno = 12; + { + PROTECT_ERRNO; + errno = 11; + } + assert_se(errno == 12); +} + +static void test_unprotect_errno_inner_function(void) { + PROTECT_ERRNO; + + errno = 2222; +} + +TEST(UNPROTECT_ERRNO) { + errno = 4711; + + PROTECT_ERRNO; + + errno = 815; + + UNPROTECT_ERRNO; + + assert_se(errno == 4711); + + test_unprotect_errno_inner_function(); + + assert_se(errno == 4711); +} + DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-exec-util.c b/src/test/test-exec-util.c index bae06a81ad..2304f6a8b6 100644 --- a/src/test/test-exec-util.c +++ b/src/test/test-exec-util.c @@ -7,7 +7,7 @@ #include "alloc-util.h" #include "copy.h" -#include "def.h" +#include "constants.h" #include "env-util.h" #include "exec-util.h" #include "fd-util.h" diff --git a/src/test/test-execute.c b/src/test/test-execute.c index 0283caeca6..51c3e755e0 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -32,7 +32,6 @@ #include "tmpfile-util.h" #include "unit.h" #include "user-util.h" -#include "util.h" #include "virt.h" static char *user_runtime_unit_dir = NULL; diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c index 5d63eeee37..b6ad290eef 100644 --- a/src/test/test-fdset.c +++ b/src/test/test-fdset.c @@ -8,7 +8,6 @@ #include "macro.h" #include "tests.h" #include "tmpfile-util.h" -#include "util.h" TEST(fdset_new_fill) { int fd = -1; diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index ba6dd9ace9..6f27cd43d6 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -24,7 +24,6 @@ #include "strv.h" #include "tests.h" #include "tmpfile-util.h" -#include "util.h" TEST(parse_env_file) { _cleanup_(unlink_tempfilep) char diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 9c1ced7591..d0259843b6 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -22,7 +22,6 @@ #include "tmpfile-util.h" #include "umask-util.h" #include "user-util.h" -#include "util.h" #include "virt.h" static const char *arg_test_dir = NULL; diff --git a/src/test/test-gpt.c b/src/test/test-gpt.c index 5037f498bb..8c313c66cc 100644 --- a/src/test/test-gpt.c +++ b/src/test/test-gpt.c @@ -8,7 +8,6 @@ #include "strv.h" #include "terminal-util.h" #include "tests.h" -#include "util.h" TEST(gpt_types_against_architectures) { int r; diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index dbf762cc0b..5daa0e64f6 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -3,7 +3,6 @@ #include "hashmap.h" #include "string-util.h" #include "tests.h" -#include "util.h" unsigned custom_counter = 0; static void custom_destruct(void* p) { diff --git a/src/test/test-json.c b/src/test/test-json.c index 946c827ccf..17ad2017f8 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -12,7 +12,6 @@ #include "string-util.h" #include "strv.h" #include "tests.h" -#include "util.h" static void test_tokenizer_one(const char *data, ...) { unsigned line = 0, column = 0; diff --git a/src/test/test-limits-util.c b/src/test/test-limits-util.c new file mode 100644 index 0000000000..3b6c8c0224 --- /dev/null +++ b/src/test/test-limits-util.c @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "format-util.h" +#include "limits-util.h" +#include "tests.h" + +TEST(physical_memory) { + uint64_t p; + + p = physical_memory(); + assert_se(p > 0); + assert_se(p < UINT64_MAX); + assert_se(p % page_size() == 0); + + log_info("Memory: %s (%" PRIu64 ")", FORMAT_BYTES(p), p); +} + +TEST(physical_memory_scale) { + uint64_t p; + + p = physical_memory(); + + assert_se(physical_memory_scale(0, 100) == 0); + assert_se(physical_memory_scale(100, 100) == p); + + log_info("Memory original: %" PRIu64, physical_memory()); + log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100)); + log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2); + log_info("Page size: %zu", page_size()); + + /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */ + assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size()); + assert_se(physical_memory_scale(200, 100) == p*2); + + assert_se(physical_memory_scale(0, 1) == 0); + assert_se(physical_memory_scale(1, 1) == p); + assert_se(physical_memory_scale(2, 1) == p*2); + + assert_se(physical_memory_scale(0, 2) == 0); + + assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size()); + assert_se(physical_memory_scale(2, 2) == p); + assert_se(physical_memory_scale(4, 2) == p*2); + + assert_se(physical_memory_scale(0, UINT32_MAX) == 0); + assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p); + + /* overflow */ + assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); +} + +TEST(system_tasks_max) { + uint64_t t; + + t = system_tasks_max(); + assert_se(t > 0); + assert_se(t < UINT64_MAX); + + log_info("Max tasks: %" PRIu64, t); +} + +TEST(system_tasks_max_scale) { + uint64_t t; + + t = system_tasks_max(); + + assert_se(system_tasks_max_scale(0, 100) == 0); + assert_se(system_tasks_max_scale(100, 100) == t); + + assert_se(system_tasks_max_scale(0, 1) == 0); + assert_se(system_tasks_max_scale(1, 1) == t); + assert_se(system_tasks_max_scale(2, 1) == 2*t); + + assert_se(system_tasks_max_scale(0, 2) == 0); + assert_se(system_tasks_max_scale(1, 2) == t/2); + assert_se(system_tasks_max_scale(2, 2) == t); + assert_se(system_tasks_max_scale(3, 2) == (3*t)/2); + assert_se(system_tasks_max_scale(4, 2) == t*2); + + assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0); + assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2); + assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t); + + /* overflow */ + + assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); +} + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-list.c b/src/test/test-list.c index 2c764d7b71..ea45f5b95c 100644 --- a/src/test/test-list.c +++ b/src/test/test-list.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "list.h" -#include "util.h" int main(int argc, const char *argv[]) { size_t i; diff --git a/src/test/test-locale-util.c b/src/test/test-locale-util.c index 9f50c6227f..aa501b650a 100644 --- a/src/test/test-locale-util.c +++ b/src/test/test-locale-util.c @@ -6,7 +6,6 @@ #include "macro.h" #include "strv.h" #include "tests.h" -#include "util.h" TEST(get_locales) { _cleanup_strv_free_ char **locales = NULL; diff --git a/src/test/test-log.c b/src/test/test-log.c index ae3d073d82..f21d88f39a 100644 --- a/src/test/test-log.c +++ b/src/test/test-log.c @@ -7,7 +7,6 @@ #include "log.h" #include "process-util.h" #include "string-util.h" -#include "util.h" assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL))); assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL)); diff --git a/src/test/test-logarithm.c b/src/test/test-logarithm.c new file mode 100644 index 0000000000..b6818b422c --- /dev/null +++ b/src/test/test-logarithm.c @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "logarithm.h" +#include "tests.h" + +TEST(LOG2ULL) { + assert_se(LOG2ULL(0) == 0); + assert_se(LOG2ULL(1) == 0); + assert_se(LOG2ULL(8) == 3); + assert_se(LOG2ULL(9) == 3); + assert_se(LOG2ULL(15) == 3); + assert_se(LOG2ULL(16) == 4); + assert_se(LOG2ULL(1024*1024) == 20); + assert_se(LOG2ULL(1024*1024+5) == 20); +} + +TEST(CONST_LOG2ULL) { + assert_se(CONST_LOG2ULL(0) == 0); + assert_se(CONST_LOG2ULL(1) == 0); + assert_se(CONST_LOG2ULL(8) == 3); + assert_se(CONST_LOG2ULL(9) == 3); + assert_se(CONST_LOG2ULL(15) == 3); + assert_se(CONST_LOG2ULL(16) == 4); + assert_se(CONST_LOG2ULL(1024*1024) == 20); + assert_se(CONST_LOG2ULL(1024*1024+5) == 20); +} + +TEST(NONCONST_LOG2ULL) { + assert_se(NONCONST_LOG2ULL(0) == 0); + assert_se(NONCONST_LOG2ULL(1) == 0); + assert_se(NONCONST_LOG2ULL(8) == 3); + assert_se(NONCONST_LOG2ULL(9) == 3); + assert_se(NONCONST_LOG2ULL(15) == 3); + assert_se(NONCONST_LOG2ULL(16) == 4); + assert_se(NONCONST_LOG2ULL(1024*1024) == 20); + assert_se(NONCONST_LOG2ULL(1024*1024+5) == 20); +} + +TEST(log2u64) { + assert_se(log2u64(0) == 0); + assert_se(log2u64(1) == 0); + assert_se(log2u64(8) == 3); + assert_se(log2u64(9) == 3); + assert_se(log2u64(15) == 3); + assert_se(log2u64(16) == 4); + assert_se(log2u64(1024*1024) == 20); + assert_se(log2u64(1024*1024+5) == 20); +} + +TEST(log2u) { + assert_se(log2u(0) == 0); + assert_se(log2u(1) == 0); + assert_se(log2u(2) == 1); + assert_se(log2u(3) == 1); + assert_se(log2u(4) == 2); + assert_se(log2u(32) == 5); + assert_se(log2u(33) == 5); + assert_se(log2u(63) == 5); + assert_se(log2u(INT_MAX) == sizeof(int)*8-2); +} + +TEST(log2i) { + assert_se(log2i(0) == 0); + assert_se(log2i(1) == 0); + assert_se(log2i(2) == 1); + assert_se(log2i(3) == 1); + assert_se(log2i(4) == 2); + assert_se(log2i(32) == 5); + assert_se(log2i(33) == 5); + assert_se(log2i(63) == 5); + assert_se(log2i(INT_MAX) == sizeof(int)*8-2); +} + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-memory-util.c b/src/test/test-memory-util.c new file mode 100644 index 0000000000..241f46c0d0 --- /dev/null +++ b/src/test/test-memory-util.c @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "memory-util.h" +#include "tests.h" + +TEST(eqzero) { + const uint32_t zeros[] = {0, 0, 0}; + const uint32_t ones[] = {1, 1}; + const uint32_t mixed[] = {0, 1, 0, 0, 0}; + const uint8_t longer[] = {[55] = 255}; + + assert_se(eqzero(zeros)); + assert_se(!eqzero(ones)); + assert_se(!eqzero(mixed)); + assert_se(!eqzero(longer)); +} + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index 391e1c97ba..ccf18ecd9c 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -5,7 +5,7 @@ #include <unistd.h> #include "alloc-util.h" -#include "def.h" +#include "constants.h" #include "fd-util.h" #include "fileio.h" #include "hashmap.h" diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c index 37acc782eb..e4d763ecd5 100644 --- a/src/test/test-namespace.c +++ b/src/test/test-namespace.c @@ -11,7 +11,6 @@ #include "string-util.h" #include "tests.h" #include "user-util.h" -#include "util.h" #include "virt.h" TEST(namespace_cleanup_tmpdir) { diff --git a/src/test/test-netlink-manual.c b/src/test/test-netlink-manual.c index 49aca68c49..6543c617c8 100644 --- a/src/test/test-netlink-manual.c +++ b/src/test/test-netlink-manual.c @@ -11,7 +11,6 @@ #include "macro.h" #include "module-util.h" #include "tests.h" -#include "util.h" static int load_module(const char *mod_name) { _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL; diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index f398761982..06b9793fe3 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -16,7 +16,6 @@ #include "strv.h" #include "tests.h" #include "tmpfile-util.h" -#include "util.h" TEST(print_paths) { log_info("DEFAULT_PATH=%s", DEFAULT_PATH); diff --git a/src/test/test-path.c b/src/test/test-path.c index 4066f6ad93..f325b4e51a 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -17,7 +17,6 @@ #include "strv.h" #include "tests.h" #include "unit.h" -#include "util.h" typedef void (*test_function_t)(Manager *m); diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index 1f43bb3eb0..1d54066ae6 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -3,13 +3,13 @@ #include "alloc-util.h" #include "env-util.h" #include "errno-util.h" +#include "initrd-util.h" #include "log.h" #include "macro.h" #include "proc-cmdline.h" #include "special.h" #include "string-util.h" #include "tests.h" -#include "util.h" static int obj; diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 990eb1e2b6..7e4b5d8255 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -34,7 +34,6 @@ #include "terminal-util.h" #include "tests.h" #include "user-util.h" -#include "util.h" #include "virt.h" static void test_get_process_comm_one(pid_t pid) { @@ -524,108 +523,6 @@ TEST(get_process_cmdline_harder) { _exit(EXIT_SUCCESS); } -static void test_rename_process_now(const char *p, int ret) { - _cleanup_free_ char *comm = NULL, *cmdline = NULL; - int r; - - log_info("/* %s(%s) */", __func__, p); - - r = rename_process(p); - assert_se(r == ret || - (ret == 0 && r >= 0) || - (ret > 0 && r > 0)); - - log_debug_errno(r, "rename_process(%s): %m", p); - - if (r < 0) - return; - -#if HAVE_VALGRIND_VALGRIND_H - /* see above, valgrind is weird, we can't verify what we are doing here */ - if (RUNNING_ON_VALGRIND) - return; -#endif - - assert_se(get_process_comm(0, &comm) >= 0); - log_debug("comm = <%s>", comm); - assert_se(strneq(comm, p, TASK_COMM_LEN-1)); - /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the - * future. We'd only check the initial part, at least until we recompile, but this will still pass. */ - - r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline); - assert_se(r >= 0); - /* we cannot expect cmdline to be renamed properly without privileges */ - if (geteuid() == 0) { - if (r == 0 && detect_container() > 0) - log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline); - else { - log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p); - - bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */ - - assert_se(strneq(cmdline + skip, p, strlen("test-process-util"))); - assert_se(startswith(cmdline + skip, p)); - } - } else - log_info("cmdline = <%s> (not verified)", cmdline); -} - -static void test_rename_process_one(const char *p, int ret) { - siginfo_t si; - pid_t pid; - - log_info("/* %s(%s) */", __func__, p); - - pid = fork(); - assert_se(pid >= 0); - - if (pid == 0) { - /* child */ - test_rename_process_now(p, ret); - _exit(EXIT_SUCCESS); - } - - assert_se(wait_for_terminate(pid, &si) >= 0); - assert_se(si.si_code == CLD_EXITED); - assert_se(si.si_status == EXIT_SUCCESS); -} - -TEST(rename_process_invalid) { - assert_se(rename_process(NULL) == -EINVAL); - assert_se(rename_process("") == -EINVAL); -} - -TEST(rename_process_multi) { - pid_t pid; - - pid = fork(); - assert_se(pid >= 0); - - if (pid > 0) { - siginfo_t si; - - assert_se(wait_for_terminate(pid, &si) >= 0); - assert_se(si.si_code == CLD_EXITED); - assert_se(si.si_status == EXIT_SUCCESS); - - return; - } - - /* child */ - test_rename_process_now("one", 1); - test_rename_process_now("more", 0); /* longer than "one", hence truncated */ - (void) setresuid(99, 99, 99); /* change uid when running privileged */ - test_rename_process_now("time!", 0); - test_rename_process_now("0", 1); /* shorter than "one", should fit */ - _exit(EXIT_SUCCESS); -} - -TEST(rename_process) { - test_rename_process_one("foo", 1); /* should always fit */ - test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ - test_rename_process_one("1234567", 1); /* should always fit */ -} - TEST(getpid_cached) { siginfo_t si; pid_t a, b, c, d, e, f, child; diff --git a/src/test/test-raw-clone.c b/src/test/test-raw-clone.c new file mode 100644 index 0000000000..23ec7d1aa0 --- /dev/null +++ b/src/test/test-raw-clone.c @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include <sys/wait.h> +#include <unistd.h> + +#include "errno-util.h" +#include "format-util.h" +#include "missing_syscall.h" +#include "raw-clone.h" +#include "tests.h" + +TEST(raw_clone) { + pid_t parent, pid, pid2; + + parent = getpid(); + log_info("before clone: getpid()→"PID_FMT, parent); + assert_se(raw_getpid() == parent); + + pid = raw_clone(0); + assert_se(pid >= 0); + + pid2 = raw_getpid(); + log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT, + pid, getpid(), pid2); + if (pid == 0) { + assert_se(pid2 != parent); + _exit(EXIT_SUCCESS); + } else { + int status; + + assert_se(pid2 == parent); + waitpid(pid, &status, __WCLONE); + assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + } + + errno = 0; + assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1); + assert_se(errno == EINVAL || ERRNO_IS_PRIVILEGE(errno)); /* Certain container environments prohibit namespaces to us, don't fail in that case */ +} + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-selinux.c b/src/test/test-selinux.c index 3eb7ad30cf..717cb0db16 100644 --- a/src/test/test-selinux.c +++ b/src/test/test-selinux.c @@ -9,7 +9,6 @@ #include "string-util.h" #include "tests.h" #include "time-util.h" -#include "util.h" static void test_testing(void) { bool b; diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c index 799e93dced..de64f920d1 100644 --- a/src/test/test-sleep.c +++ b/src/test/test-sleep.c @@ -15,7 +15,6 @@ #include "sleep-config.h" #include "strv.h" #include "tests.h" -#include "util.h" TEST(parse_sleep_config) { _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL; diff --git a/src/test/test-strbuf.c b/src/test/test-strbuf.c index 9233c63f90..5c12a0597a 100644 --- a/src/test/test-strbuf.c +++ b/src/test/test-strbuf.c @@ -6,7 +6,6 @@ #include "string-util.h" #include "strv.h" #include "tests.h" -#include "util.h" static ssize_t add_string(struct strbuf *sb, const char *s) { return strbuf_add_string(sb, s, strlen(s)); diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index d0a2272794..4047139c26 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -9,7 +9,6 @@ #include "strv.h" #include "tests.h" #include "utf8.h" -#include "util.h" TEST(string_erase) { char *x; diff --git a/src/test/test-strip-tab-ansi.c b/src/test/test-strip-tab-ansi.c index 5152cf2cf8..6f73d2686d 100644 --- a/src/test/test-strip-tab-ansi.c +++ b/src/test/test-strip-tab-ansi.c @@ -7,7 +7,6 @@ #include "string-util.h" #include "terminal-util.h" #include "tests.h" -#include "util.h" TEST(strip_tab_ansi) { _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL; diff --git a/src/test/test-strxcpyx.c b/src/test/test-strxcpyx.c index dd8dbdea61..b679522fc6 100644 --- a/src/test/test-strxcpyx.c +++ b/src/test/test-strxcpyx.c @@ -5,7 +5,6 @@ #include "string-util.h" #include "strxcpyx.h" #include "tests.h" -#include "util.h" TEST(strpcpy) { char target[25]; diff --git a/src/test/test-tables.c b/src/test/test-tables.c index 30ca1871cb..6301dedb09 100644 --- a/src/test/test-tables.c +++ b/src/test/test-tables.c @@ -36,7 +36,6 @@ #include "timer.h" #include "unit-name.h" #include "unit.h" -#include "util.h" #include "virt.h" int main(int argc, char **argv) { diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 292e5348bc..8d0e452038 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -14,7 +14,6 @@ #include "terminal-util.h" #include "tests.h" #include "tmpfile-util.h" -#include "util.h" #define LOREM_IPSUM "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " \ "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \ diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c index f26701767f..c58ac933bb 100644 --- a/src/test/test-tmpfiles.c +++ b/src/test/test-tmpfiles.c @@ -15,7 +15,6 @@ #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" -#include "util.h" TEST(tmpfiles) { _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL; diff --git a/src/test/test-uid-range.c b/src/test/test-uid-range.c index ce8b8e4bca..c759573173 100644 --- a/src/test/test-uid-range.c +++ b/src/test/test-uid-range.c @@ -11,7 +11,6 @@ #include "tmpfile-util.h" #include "uid-range.h" #include "user-util.h" -#include "util.h" #include "virt.h" TEST(uid_range) { diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index dffa2822e6..27a73d61a0 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "initrd-util.h" #include "path-lookup.h" #include "set.h" #include "special.h" diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index 43fdb15d1c..98fffc7c87 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -22,7 +22,6 @@ #include "unit-printf.h" #include "unit.h" #include "user-util.h" -#include "util.h" static char *runtime_dir = NULL; diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c index f070c171fe..7ba0cb7dd2 100644 --- a/src/test/test-utf8.c +++ b/src/test/test-utf8.c @@ -5,7 +5,6 @@ #include "strv.h" #include "tests.h" #include "utf8.h" -#include "util.h" TEST(utf8_is_printable) { assert_se(utf8_is_printable("ascii is valid\tunicode", 22)); diff --git a/src/test/test-util.c b/src/test/test-util.c deleted file mode 100644 index 21ab016c22..0000000000 --- a/src/test/test-util.c +++ /dev/null @@ -1,242 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ - -#include <errno.h> -#include <sys/wait.h> -#include <unistd.h> - -#include "fileio.h" -#include "fs-util.h" -#include "limits-util.h" -#include "memory-util.h" -#include "missing_syscall.h" -#include "parse-util.h" -#include "process-util.h" -#include "raw-clone.h" -#include "rm-rf.h" -#include "string-util.h" -#include "tests.h" -#include "util.h" - -TEST(LOG2ULL) { - assert_se(LOG2ULL(0) == 0); - assert_se(LOG2ULL(1) == 0); - assert_se(LOG2ULL(8) == 3); - assert_se(LOG2ULL(9) == 3); - assert_se(LOG2ULL(15) == 3); - assert_se(LOG2ULL(16) == 4); - assert_se(LOG2ULL(1024*1024) == 20); - assert_se(LOG2ULL(1024*1024+5) == 20); -} - -TEST(CONST_LOG2ULL) { - assert_se(CONST_LOG2ULL(0) == 0); - assert_se(CONST_LOG2ULL(1) == 0); - assert_se(CONST_LOG2ULL(8) == 3); - assert_se(CONST_LOG2ULL(9) == 3); - assert_se(CONST_LOG2ULL(15) == 3); - assert_se(CONST_LOG2ULL(16) == 4); - assert_se(CONST_LOG2ULL(1024*1024) == 20); - assert_se(CONST_LOG2ULL(1024*1024+5) == 20); -} - -TEST(NONCONST_LOG2ULL) { - assert_se(NONCONST_LOG2ULL(0) == 0); - assert_se(NONCONST_LOG2ULL(1) == 0); - assert_se(NONCONST_LOG2ULL(8) == 3); - assert_se(NONCONST_LOG2ULL(9) == 3); - assert_se(NONCONST_LOG2ULL(15) == 3); - assert_se(NONCONST_LOG2ULL(16) == 4); - assert_se(NONCONST_LOG2ULL(1024*1024) == 20); - assert_se(NONCONST_LOG2ULL(1024*1024+5) == 20); -} - -TEST(log2u64) { - assert_se(log2u64(0) == 0); - assert_se(log2u64(1) == 0); - assert_se(log2u64(8) == 3); - assert_se(log2u64(9) == 3); - assert_se(log2u64(15) == 3); - assert_se(log2u64(16) == 4); - assert_se(log2u64(1024*1024) == 20); - assert_se(log2u64(1024*1024+5) == 20); -} - -TEST(log2u) { - assert_se(log2u(0) == 0); - assert_se(log2u(1) == 0); - assert_se(log2u(2) == 1); - assert_se(log2u(3) == 1); - assert_se(log2u(4) == 2); - assert_se(log2u(32) == 5); - assert_se(log2u(33) == 5); - assert_se(log2u(63) == 5); - assert_se(log2u(INT_MAX) == sizeof(int)*8-2); -} - -TEST(log2i) { - assert_se(log2i(0) == 0); - assert_se(log2i(1) == 0); - assert_se(log2i(2) == 1); - assert_se(log2i(3) == 1); - assert_se(log2i(4) == 2); - assert_se(log2i(32) == 5); - assert_se(log2i(33) == 5); - assert_se(log2i(63) == 5); - assert_se(log2i(INT_MAX) == sizeof(int)*8-2); -} - -TEST(protect_errno) { - errno = 12; - { - PROTECT_ERRNO; - errno = 11; - } - assert_se(errno == 12); -} - -static void test_unprotect_errno_inner_function(void) { - PROTECT_ERRNO; - - errno = 2222; -} - -TEST(unprotect_errno) { - errno = 4711; - - PROTECT_ERRNO; - - errno = 815; - - UNPROTECT_ERRNO; - - assert_se(errno == 4711); - - test_unprotect_errno_inner_function(); - - assert_se(errno == 4711); -} - -TEST(eqzero) { - const uint32_t zeros[] = {0, 0, 0}; - const uint32_t ones[] = {1, 1}; - const uint32_t mixed[] = {0, 1, 0, 0, 0}; - const uint8_t longer[] = {[55] = 255}; - - assert_se(eqzero(zeros)); - assert_se(!eqzero(ones)); - assert_se(!eqzero(mixed)); - assert_se(!eqzero(longer)); -} - -TEST(raw_clone) { - pid_t parent, pid, pid2; - - parent = getpid(); - log_info("before clone: getpid()→"PID_FMT, parent); - assert_se(raw_getpid() == parent); - - pid = raw_clone(0); - assert_se(pid >= 0); - - pid2 = raw_getpid(); - log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT, - pid, getpid(), pid2); - if (pid == 0) { - assert_se(pid2 != parent); - _exit(EXIT_SUCCESS); - } else { - int status; - - assert_se(pid2 == parent); - waitpid(pid, &status, __WCLONE); - assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); - } - - errno = 0; - assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1); - assert_se(errno == EINVAL || ERRNO_IS_PRIVILEGE(errno)); /* Certain container environments prohibit namespaces to us, don't fail in that case */ -} - -TEST(physical_memory) { - uint64_t p; - - p = physical_memory(); - assert_se(p > 0); - assert_se(p < UINT64_MAX); - assert_se(p % page_size() == 0); - - log_info("Memory: %s (%" PRIu64 ")", FORMAT_BYTES(p), p); -} - -TEST(physical_memory_scale) { - uint64_t p; - - p = physical_memory(); - - assert_se(physical_memory_scale(0, 100) == 0); - assert_se(physical_memory_scale(100, 100) == p); - - log_info("Memory original: %" PRIu64, physical_memory()); - log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100)); - log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2); - log_info("Page size: %zu", page_size()); - - /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */ - assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size()); - assert_se(physical_memory_scale(200, 100) == p*2); - - assert_se(physical_memory_scale(0, 1) == 0); - assert_se(physical_memory_scale(1, 1) == p); - assert_se(physical_memory_scale(2, 1) == p*2); - - assert_se(physical_memory_scale(0, 2) == 0); - - assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size()); - assert_se(physical_memory_scale(2, 2) == p); - assert_se(physical_memory_scale(4, 2) == p*2); - - assert_se(physical_memory_scale(0, UINT32_MAX) == 0); - assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p); - - /* overflow */ - assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); -} - -TEST(system_tasks_max) { - uint64_t t; - - t = system_tasks_max(); - assert_se(t > 0); - assert_se(t < UINT64_MAX); - - log_info("Max tasks: %" PRIu64, t); -} - -TEST(system_tasks_max_scale) { - uint64_t t; - - t = system_tasks_max(); - - assert_se(system_tasks_max_scale(0, 100) == 0); - assert_se(system_tasks_max_scale(100, 100) == t); - - assert_se(system_tasks_max_scale(0, 1) == 0); - assert_se(system_tasks_max_scale(1, 1) == t); - assert_se(system_tasks_max_scale(2, 1) == 2*t); - - assert_se(system_tasks_max_scale(0, 2) == 0); - assert_se(system_tasks_max_scale(1, 2) == t/2); - assert_se(system_tasks_max_scale(2, 2) == t); - assert_se(system_tasks_max_scale(3, 2) == (3*t)/2); - assert_se(system_tasks_max_scale(4, 2) == t*2); - - assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0); - assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2); - assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t); - - /* overflow */ - - assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); -} - -DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-xml.c b/src/test/test-xml.c index e69d6d0fe4..5dee5c1bed 100644 --- a/src/test/test-xml.c +++ b/src/test/test-xml.c @@ -4,7 +4,6 @@ #include "alloc-util.h" #include "string-util.h" -#include "util.h" #include "xml.h" static void test_one(const char *data, ...) { |