ryantimwilson / rpms / systemd

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