dd65c9
From 4a0e2c447eeac47eaa497a2db6925590b3cec3bd Mon Sep 17 00:00:00 2001
dd65c9
From: Jan Synacek <jsynacek@redhat.com>
dd65c9
Date: Thu, 23 Nov 2017 11:42:05 +0100
dd65c9
Subject: [PATCH] test-fileio: also test read_line() with actual files
dd65c9
dd65c9
Just in case the real FILE and the one from fmemopen weren't exactly
dd65c9
the same.
dd65c9
dd65c9
(cherry picked from commit 2c9de13912350f5887ccccdae9e1707512208053)
dd65c9
dd65c9
Resolves: #1503106
dd65c9
---
dd65c9
 src/test/test-fileio.c | 63 ++++++++++++++++++++++++++++++++++++++++----------
dd65c9
 1 file changed, 51 insertions(+), 12 deletions(-)
dd65c9
dd65c9
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
dd65c9
index fc5969322..791bfc97b 100644
dd65c9
--- a/src/test/test-fileio.c
dd65c9
+++ b/src/test/test-fileio.c
dd65c9
@@ -392,20 +392,17 @@ static void test_load_env_file_pairs(void) {
dd65c9
         unlink(fn);
dd65c9
 }
dd65c9
 
dd65c9
-static void test_read_line(void) {
dd65c9
-        _cleanup_fclose_ FILE *f = NULL;
dd65c9
-        _cleanup_free_ char *line = NULL;
dd65c9
 
dd65c9
-        char buffer[] =
dd65c9
-                "Some test data\n"
dd65c9
-                "With newlines, and a NUL byte\0"
dd65c9
-                "\n"
dd65c9
-                "an empty line\n"
dd65c9
-                "an ignored line\n"
dd65c9
-                "and a very long line that is supposed to be truncated, because it is so long\n";
dd65c9
+static const char buffer[] =
dd65c9
+        "Some test data\n"
dd65c9
+        "With newlines, and a NUL byte\0"
dd65c9
+        "\n"
dd65c9
+        "an empty line\n"
dd65c9
+        "an ignored line\n"
dd65c9
+        "and a very long line that is supposed to be truncated, because it is so long\n";
dd65c9
 
dd65c9
-        f = fmemopen(buffer, sizeof(buffer), "re");
dd65c9
-        assert_se(f);
dd65c9
+static void test_read_line_one_file(FILE *f) {
dd65c9
+        _cleanup_free_ char *line = NULL;
dd65c9
 
dd65c9
         assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
dd65c9
         line = mfree(line);
dd65c9
@@ -435,6 +432,46 @@ static void test_read_line(void) {
dd65c9
         assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
dd65c9
 }
dd65c9
 
dd65c9
+static void test_read_line(void) {
dd65c9
+        _cleanup_fclose_ FILE *f = NULL;
dd65c9
+        _cleanup_free_ char *line = NULL;
dd65c9
+
dd65c9
+        f = fmemopen((void*) buffer, sizeof(buffer), "re");
dd65c9
+        assert_se(f);
dd65c9
+
dd65c9
+        test_read_line_one_file(f);
dd65c9
+}
dd65c9
+
dd65c9
+static void test_read_line2(void) {
dd65c9
+        char name[] = "/tmp/test-fileio.XXXXXX";
dd65c9
+        int fd;
dd65c9
+        _cleanup_fclose_ FILE *f = NULL;
dd65c9
+
dd65c9
+        fd = mkostemp_safe(name, O_CLOEXEC);
dd65c9
+        assert_se(fd >= 0);
dd65c9
+        assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
dd65c9
+
dd65c9
+        assert_se(lseek(fd, 0, SEEK_SET) == 0);
dd65c9
+        assert_se(f = fdopen(fd, "r"));
dd65c9
+
dd65c9
+        test_read_line_one_file(f);
dd65c9
+}
dd65c9
+
dd65c9
+static void test_read_line3(void) {
dd65c9
+        _cleanup_fclose_ FILE *f = NULL;
dd65c9
+        _cleanup_free_ char *line = NULL;
dd65c9
+        int r;
dd65c9
+
dd65c9
+        f = fopen("/proc/cmdline", "re");
dd65c9
+        if (!f && IN_SET(errno, ENOENT, EPERM))
dd65c9
+                return;
dd65c9
+        assert_se(f);
dd65c9
+
dd65c9
+        r = read_line(f, LINE_MAX, &line);
dd65c9
+        assert_se((size_t) r == strlen(line) + 1);
dd65c9
+        assert_se(read_line(f, LINE_MAX, NULL) == 0);
dd65c9
+}
dd65c9
+
dd65c9
 int main(int argc, char *argv[]) {
dd65c9
         log_parse_environment();
dd65c9
         log_open();
dd65c9
@@ -449,6 +486,8 @@ int main(int argc, char *argv[]) {
dd65c9
         test_write_string_file_no_create();
dd65c9
         test_load_env_file_pairs();
dd65c9
         test_read_line();
dd65c9
+        test_read_line2();
dd65c9
+        test_read_line3();
dd65c9
 
dd65c9
         return 0;
dd65c9
 }