Merge pull request #25291 from keszybz/util-cleanup

Split/rename util.c+h and def.h
This commit is contained in:
Yu Watanabe
2022-11-09 09:23:17 +09:00
committed by GitHub
361 changed files with 1107 additions and 1080 deletions
+9 -1
View File
@@ -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'),
-1
View File
@@ -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);
-1
View File
@@ -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[]) {
+123
View File
@@ -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);
-1
View File
@@ -6,7 +6,6 @@
#include "async.h"
#include "macro.h"
#include "tmpfile-util.h"
#include "util.h"
static bool test_async = false;
-1
View File
@@ -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
-1
View File
@@ -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;
-1
View File
@@ -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;
-1
View File
@@ -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) {
-1
View File
@@ -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) {
-1
View File
@@ -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;
-1
View File
@@ -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;
-1
View File
@@ -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) {
+1 -2
View File
@@ -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) {
-1
View File
@@ -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;
-1
View File
@@ -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++) {
+31
View File
@@ -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);
+1 -1
View File
@@ -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"
-1
View File
@@ -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;
-1
View File
@@ -8,7 +8,6 @@
#include "macro.h"
#include "tests.h"
#include "tmpfile-util.h"
#include "util.h"
TEST(fdset_new_fill) {
int fd = -1;
-1
View File
@@ -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
-1
View File
@@ -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;
-1
View File
@@ -8,7 +8,6 @@
#include "strv.h"
#include "terminal-util.h"
#include "tests.h"
#include "util.h"
TEST(gpt_types_against_architectures) {
int r;
-1
View File
@@ -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) {
-1
View File
@@ -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;
+89
View File
@@ -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);
-1
View File
@@ -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;
-1
View File
@@ -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;
-1
View File
@@ -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));
+74
View File
@@ -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);
+18
View File
@@ -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);
+1 -1
View File
@@ -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"
-1
View File
@@ -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) {
-1
View File
@@ -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;
-1
View File
@@ -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);
-1
View File
@@ -17,7 +17,6 @@
#include "strv.h"
#include "tests.h"
#include "unit.h"
#include "util.h"
typedef void (*test_function_t)(Manager *m);
+1 -1
View File
@@ -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;
-103
View File
@@ -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;
+41
View File
@@ -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);
-1
View File
@@ -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;
-1
View File
@@ -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;
-1
View File
@@ -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));
-1
View File
@@ -9,7 +9,6 @@
#include "strv.h"
#include "tests.h"
#include "utf8.h"
#include "util.h"
TEST(string_erase) {
char *x;
-1
View File
@@ -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;
-1
View File
@@ -5,7 +5,6 @@
#include "string-util.h"
#include "strxcpyx.h"
#include "tests.h"
#include "util.h"
TEST(strpcpy) {
char target[25];
-1
View File
@@ -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) {
-1
View File
@@ -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 " \
-1
View File
@@ -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;
-1
View File
@@ -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) {
+1
View File
@@ -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"
-1
View File
@@ -22,7 +22,6 @@
#include "unit-printf.h"
#include "unit.h"
#include "user-util.h"
#include "util.h"
static char *runtime_dir = NULL;
-1
View File
@@ -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));
-242
View File
@@ -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);
-1
View File
@@ -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, ...) {