anitazha / rpms / systemd

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