ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
Blob Blame History Raw
From 4a0e2c447eeac47eaa497a2db6925590b3cec3bd Mon Sep 17 00:00:00 2001
From: Jan Synacek <jsynacek@redhat.com>
Date: Thu, 23 Nov 2017 11:42:05 +0100
Subject: [PATCH] test-fileio: also test read_line() with actual files

Just in case the real FILE and the one from fmemopen weren't exactly
the same.

(cherry picked from commit 2c9de13912350f5887ccccdae9e1707512208053)

Resolves: #1503106
---
 src/test/test-fileio.c | 63 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 12 deletions(-)

diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index fc5969322..791bfc97b 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -392,20 +392,17 @@ static void test_load_env_file_pairs(void) {
         unlink(fn);
 }
 
-static void test_read_line(void) {
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *line = NULL;
 
-        char buffer[] =
-                "Some test data\n"
-                "With newlines, and a NUL byte\0"
-                "\n"
-                "an empty line\n"
-                "an ignored line\n"
-                "and a very long line that is supposed to be truncated, because it is so long\n";
+static const char buffer[] =
+        "Some test data\n"
+        "With newlines, and a NUL byte\0"
+        "\n"
+        "an empty line\n"
+        "an ignored line\n"
+        "and a very long line that is supposed to be truncated, because it is so long\n";
 
-        f = fmemopen(buffer, sizeof(buffer), "re");
-        assert_se(f);
+static void test_read_line_one_file(FILE *f) {
+        _cleanup_free_ char *line = NULL;
 
         assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
         line = mfree(line);
@@ -435,6 +432,46 @@ static void test_read_line(void) {
         assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
 }
 
+static void test_read_line(void) {
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *line = NULL;
+
+        f = fmemopen((void*) buffer, sizeof(buffer), "re");
+        assert_se(f);
+
+        test_read_line_one_file(f);
+}
+
+static void test_read_line2(void) {
+        char name[] = "/tmp/test-fileio.XXXXXX";
+        int fd;
+        _cleanup_fclose_ FILE *f = NULL;
+
+        fd = mkostemp_safe(name, O_CLOEXEC);
+        assert_se(fd >= 0);
+        assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
+
+        assert_se(lseek(fd, 0, SEEK_SET) == 0);
+        assert_se(f = fdopen(fd, "r"));
+
+        test_read_line_one_file(f);
+}
+
+static void test_read_line3(void) {
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *line = NULL;
+        int r;
+
+        f = fopen("/proc/cmdline", "re");
+        if (!f && IN_SET(errno, ENOENT, EPERM))
+                return;
+        assert_se(f);
+
+        r = read_line(f, LINE_MAX, &line);
+        assert_se((size_t) r == strlen(line) + 1);
+        assert_se(read_line(f, LINE_MAX, NULL) == 0);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -449,6 +486,8 @@ int main(int argc, char *argv[]) {
         test_write_string_file_no_create();
         test_load_env_file_pairs();
         test_read_line();
+        test_read_line2();
+        test_read_line3();
 
         return 0;
 }