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