From 30b84c78eac027819eafc25e7f0486f2079bd63e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 13 Dec 2019 12:21:11 +0100 Subject: [PATCH] test-fileio: fix bogus error when /proc/cmdline contains newlines The kernel does not sanitize /proc/cmdline. E.g. when running under qemu, it is easy to pass a string with newline by mistake. We use read_one_line_file(), so we would read only the first list of the file, and write_string_file(WRITE_STRING_FILE_VERIFY_ON_FAILURE) would fail because the target file is obviously different. Change to a kernel-generated file to avoid the issue. v2: - use /proc/version instead of /proc/uptime for attempted writes, so the test test passes even if test_write_string_file_verify() takes more than 10 ms ;] --- src/test/test-fileio.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 23c7d370d4e..32a00349acb 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -444,20 +444,20 @@ static void test_write_string_file_verify(void) { _cleanup_free_ char *buf = NULL, *buf2 = NULL; int r; - assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0); + assert_se(read_one_line_file("/proc/version", &buf) >= 0); assert_se(buf2 = strjoin(buf, "\n")); - r = write_string_file("/proc/cmdline", buf, 0); + r = write_string_file("/proc/version", buf, 0); assert_se(IN_SET(r, -EACCES, -EIO)); - r = write_string_file("/proc/cmdline", buf2, 0); + r = write_string_file("/proc/version", buf2, 0); assert_se(IN_SET(r, -EACCES, -EIO)); - assert_se(write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); - assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); + assert_se(write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); + assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); - r = write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); + r = write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); assert_se(IN_SET(r, -EACCES, -EIO)); - assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); + assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); } static void test_load_env_file_pairs(void) { @@ -757,7 +757,7 @@ static void test_read_line3(void) { _cleanup_free_ char *line = NULL; int r; - f = fopen("/proc/cmdline", "re"); + f = fopen("/proc/uptime", "re"); if (!f && IN_SET(errno, ENOENT, EPERM)) return; assert_se(f); -- 2.47.3