summaryrefslogtreecommitdiffstats
path: root/src/test/test-process-util.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-03-30 19:42:36 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-05-05 13:59:23 +0200
commit61977664e9d186287d0b85a26256cf82ae89fcbf (patch)
tree269287a6d30fd8b451a2b30d8257012a56a22023 /src/test/test-process-util.c
parenttest-utf8: hide most output by default (diff)
downloadsystemd-61977664e9d186287d0b85a26256cf82ae89fcbf.tar.xz
systemd-61977664e9d186287d0b85a26256cf82ae89fcbf.zip
basic/process-util: allow quoting of commandlines
Since the new functionality is controlled by an option, this causes no change in output yet, except tests. The login in the old branch of !(flags & PROCESS_CMDLINE_QUOTE) is essentially unmodified. But there is an important difference in behaviour: instead of unconditionally reading the whole virtual file, we now read only 'max_columns' bytes. This makes out code to write process lists quite a bit more efficient when there are processes with long command lines.
Diffstat (limited to 'src/test/test-process-util.c')
-rw-r--r--src/test/test-process-util.c45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
index 7f0a771ba7..5f148c1522 100644
--- a/src/test/test-process-util.c
+++ b/src/test/test-process-util.c
@@ -248,9 +248,15 @@ static void test_get_process_cmdline_harder(void) {
assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_info("'%s'", line);
assert_se(streq(line, "[testa]"));
line = mfree(line);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE, &line) >= 0);
+ log_info("'%s'", line);
+ assert_se(streq(line, "\"[testa]\"")); /* quoting is enabled here */
+ line = mfree(line);
+
assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
log_info("'%s'", line);
assert_se(streq(line, ""));
@@ -288,6 +294,8 @@ static void test_get_process_cmdline_harder(void) {
assert_se(streq(line, "[testa]"));
line = mfree(line);
+ /* Test with multiple arguments that don't require quoting */
+
assert_se(write(fd, "foo\0bar", 8) == 8);
assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
@@ -390,6 +398,32 @@ static void test_get_process_cmdline_harder(void) {
assert_se(streq(line, "[aaaa bbbb …"));
line = mfree(line);
+ /* Test with multiple arguments that do require quoting */
+
+#define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
+#define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\""
+ assert_se(lseek(fd, SEEK_SET, 0) == 0);
+ assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1);
+ assert_se(ftruncate(fd, sizeof CMDLINE1) == 0);
+
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
+ log_info("got: ==%s==", line);
+ log_info("exp: ==%s==", EXPECT1);
+ assert_se(streq(line, EXPECT1));
+ line = mfree(line);
+
+#define CMDLINE2 "foo\0\1\2\3\0\0"
+#define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\""
+ assert_se(lseek(fd, SEEK_SET, 0) == 0);
+ assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2);
+ assert_se(ftruncate(fd, sizeof CMDLINE2) == 0);
+
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
+ log_info("got: ==%s==", line);
+ log_info("exp: ==%s==", EXPECT2);
+ assert_se(streq(line, EXPECT2));
+ line = mfree(line);
+
safe_close(fd);
_exit(EXIT_SUCCESS);
}
@@ -403,6 +437,8 @@ static void test_rename_process_now(const char *p, int ret) {
(ret == 0 && r >= 0) ||
(ret > 0 && r > 0));
+ log_info_errno(r, "rename_process(%s): %m", p);
+
if (r < 0)
return;
@@ -425,9 +461,12 @@ static void test_rename_process_now(const char *p, int ret) {
if (r == 0 && detect_container() > 0)
log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
else {
- log_info("cmdline = <%s>", cmdline);
- assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
- assert_se(startswith(p, cmdline));
+ 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);