summaryrefslogtreecommitdiffstats
path: root/src/test/test-process-util.c
diff options
context:
space:
mode:
authorJan Janssen <medhefgo@web.de>2021-11-24 12:11:17 +0100
committerJan Janssen <medhefgo@web.de>2021-11-25 15:03:10 +0100
commitc462e63eea7c781e00a3ce83055967dbbd67bf96 (patch)
tree60176ff67195f375edf28d8f1215e16ed649bfc0 /src/test/test-process-util.c
parenttest: Use TEST macro (diff)
downloadsystemd-c462e63eea7c781e00a3ce83055967dbbd67bf96.tar.xz
systemd-c462e63eea7c781e00a3ce83055967dbbd67bf96.zip
test: Use TEST macro in more cases
This converts to TEST macro in less trivial cases. This is mostly due to having an intro or outro before/after the actual tests. Some notable changes: - add a "test" to make sure the hashmap and ordered_hashmap tests from different compilation units are actually run in test-hashmap.c - make root arg a global var in test-install-root.c - slightly rework an EFI specific test in test-proc-cmdline.c - usage of saved_argv/saved_argc in test-process-util.c - splitting test-rlimit-util.c into several tests - moving the hwdb open check into intro in test-sd-hwdb.c - condense several "tests" into one in test-udev-util.c
Diffstat (limited to 'src/test/test-process-util.c')
-rw-r--r--src/test/test-process-util.c103
1 files changed, 30 insertions, 73 deletions
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
index cc0f870e57..a180f3f9b4 100644
--- a/src/test/test-process-util.c
+++ b/src/test/test-process-util.c
@@ -37,7 +37,7 @@
#include "util.h"
#include "virt.h"
-static void test_get_process_comm(pid_t pid) {
+static void test_get_process_comm_one(pid_t pid) {
struct stat st;
_cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
_cleanup_free_ char *env = NULL;
@@ -98,6 +98,18 @@ static void test_get_process_comm(pid_t pid) {
log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
}
+TEST(get_process_comm) {
+ if (saved_argc > 1) {
+ pid_t pid = 0;
+
+ (void) parse_pid(saved_argv[1], &pid);
+ test_get_process_comm_one(pid);
+ } else {
+ TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm_one(1));
+ test_get_process_comm_one(getpid());
+ }
+}
+
static void test_get_process_cmdline_one(pid_t pid) {
_cleanup_free_ char *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL, *h = NULL;
int r;
@@ -121,12 +133,10 @@ static void test_get_process_cmdline_one(pid_t pid) {
log_info(" %s", r >= 0 ? h : errno_to_name(r));
}
-static void test_get_process_cmdline(void) {
+TEST(get_process_cmdline) {
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
- log_info("/* %s */", __func__);
-
assert_se(d = opendir("/proc"));
FOREACH_DIRENT(de, d, return) {
@@ -155,11 +165,9 @@ static void test_get_process_comm_escape_one(const char *input, const char *outp
assert_se(streq_ptr(n, output));
}
-static void test_get_process_comm_escape(void) {
+TEST(get_process_comm_escape) {
_cleanup_free_ char *saved = NULL;
- log_info("/* %s */", __func__);
-
assert_se(get_process_comm(0, &saved) >= 0);
test_get_process_comm_escape_one("", "");
@@ -176,7 +184,7 @@ static void test_get_process_comm_escape(void) {
assert_se(prctl(PR_SET_NAME, saved) >= 0);
}
-static void test_pid_is_unwaited(void) {
+TEST(pid_is_unwaited) {
pid_t pid;
pid = fork();
@@ -193,11 +201,9 @@ static void test_pid_is_unwaited(void) {
assert_se(!pid_is_unwaited(-1));
}
-static void test_pid_is_alive(void) {
+TEST(pid_is_alive) {
pid_t pid;
- log_info("/* %s */", __func__);
-
pid = fork();
assert_se(pid >= 0);
if (pid == 0) {
@@ -212,9 +218,7 @@ static void test_pid_is_alive(void) {
assert_se(!pid_is_alive(-1));
}
-static void test_personality(void) {
- log_info("/* %s */", __func__);
-
+TEST(personality) {
assert_se(personality_to_string(PER_LINUX));
assert_se(!personality_to_string(PERSONALITY_INVALID));
@@ -236,14 +240,12 @@ static void test_personality(void) {
#endif
}
-static void test_get_process_cmdline_harder(void) {
+TEST(get_process_cmdline_harder) {
char path[] = "/tmp/test-cmdlineXXXXXX";
_cleanup_close_ int fd = -1;
_cleanup_free_ char *line = NULL;
pid_t pid;
- log_info("/* %s */", __func__);
-
if (geteuid() != 0) {
log_info("Skipping %s: not root", __func__);
return;
@@ -589,7 +591,7 @@ static void test_rename_process_one(const char *p, int ret) {
assert_se(si.si_status == EXIT_SUCCESS);
}
-static void test_rename_process_multi(void) {
+TEST(rename_process_multi) {
pid_t pid;
pid = fork();
@@ -616,21 +618,18 @@ static void test_rename_process_multi(void) {
_exit(EXIT_SUCCESS);
}
-static void test_rename_process(void) {
+TEST(rename_process) {
test_rename_process_one(NULL, -EINVAL);
test_rename_process_one("", -EINVAL);
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_rename_process_multi(); /* multiple invocations and dropped privileges */
}
-static void test_getpid_cached(void) {
+TEST(getpid_cached) {
siginfo_t si;
pid_t a, b, c, d, e, f, child;
- log_info("/* %s */", __func__);
-
a = raw_getpid();
b = getpid_cached();
c = getpid();
@@ -661,7 +660,7 @@ static void test_getpid_cached(void) {
assert_se(si.si_code == CLD_EXITED);
}
-static void test_getpid_measure(void) {
+TEST(getpid_measure) {
usec_t t, q;
unsigned long long iterations = slow_tests_enabled() ? 1000000 : 1000;
@@ -685,13 +684,11 @@ static void test_getpid_measure(void) {
log_info("getpid_cached(): %lf µs each\n", (double) q / iterations);
}
-static void test_safe_fork(void) {
+TEST(safe_fork) {
siginfo_t status;
pid_t pid;
int r;
- log_info("/* %s */", __func__);
-
BLOCK_SIGNALS(SIGCHLD);
r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
@@ -709,8 +706,7 @@ static void test_safe_fork(void) {
assert_se(status.si_status == 88);
}
-static void test_pid_to_ptr(void) {
-
+TEST(pid_to_ptr) {
assert_se(PTR_TO_PID(NULL) == 0);
assert_se(PID_TO_PTR(0) == NULL);
@@ -745,9 +741,7 @@ static void test_ioprio_class_from_to_string_one(const char *val, int expected,
}
}
-static void test_ioprio_class_from_to_string(void) {
- log_info("/* %s */", __func__);
-
+TEST(ioprio_class_from_to_string) {
test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT, IOPRIO_CLASS_RT);
test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE, IOPRIO_CLASS_BE);
@@ -760,11 +754,9 @@ static void test_ioprio_class_from_to_string(void) {
test_ioprio_class_from_to_string_one("-1", -EINVAL, -EINVAL);
}
-static void test_setpriority_closest(void) {
+TEST(setpriority_closest) {
int r;
- log_info("/* %s */", __func__);
-
r = safe_fork("(test-setprio)",
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
assert_se(r >= 0);
@@ -849,12 +841,10 @@ static void test_setpriority_closest(void) {
}
}
-static void test_get_process_ppid(void) {
+TEST(get_process_ppid) {
uint64_t limit;
int r;
- log_info("/* %s */", __func__);
-
assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL);
/* the process with the PID above the global limit definitely doesn't exist. Verify that */
@@ -888,7 +878,7 @@ static void test_get_process_ppid(void) {
}
}
-static void test_set_oom_score_adjust(void) {
+TEST(set_oom_score_adjust) {
int a, b, r;
assert_se(get_oom_score_adjust(&a) >= 0);
@@ -906,37 +896,4 @@ static void test_set_oom_score_adjust(void) {
assert_se(b == a);
}
-int main(int argc, char *argv[]) {
- log_show_color(true);
- test_setup_logging(LOG_INFO);
-
- save_argc_argv(argc, argv);
-
- if (argc > 1) {
- pid_t pid = 0;
-
- (void) parse_pid(argv[1], &pid);
- test_get_process_comm(pid);
- } else {
- TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
- test_get_process_comm(getpid());
- }
-
- test_get_process_comm_escape();
- test_get_process_cmdline();
- test_pid_is_unwaited();
- test_pid_is_alive();
- test_personality();
- test_get_process_cmdline_harder();
- test_rename_process();
- test_getpid_cached();
- test_getpid_measure();
- test_safe_fork();
- test_pid_to_ptr();
- test_ioprio_class_from_to_string();
- test_setpriority_closest();
- test_get_process_ppid();
- test_set_oom_score_adjust();
-
- return 0;
-}
+DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */);