]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: add a test that checks read_line() properly handles line endings at EOF 11160/head
authorLennart Poettering <lennart@poettering.net>
Fri, 14 Dec 2018 11:57:32 +0000 (12:57 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 14 Dec 2018 11:57:32 +0000 (12:57 +0100)
As requested here: https://github.com/systemd/systemd/pull/11129#discussion_r241588835

src/test/test-fileio.c

index 53c791814ef4a49856b1e48364625cf99401f968..fd0cc27de83dcaae885d3641c384d5c93f9f51d6 100644 (file)
@@ -713,6 +713,41 @@ static void test_read_line3(void) {
         assert_se(read_line(f, LINE_MAX, NULL) == 0);
 }
 
+static void test_read_line4(void) {
+        static const struct {
+                size_t length;
+                const char *string;
+        } eof_endings[] = {
+                /* Each of these will be followed by EOF and should generate the one same single string */
+                { 3, "foo" },
+                { 4, "foo\n" },
+                { 4, "foo\r" },
+                { 4, "foo\0" },
+                { 5, "foo\n\0" },
+                { 5, "foo\r\0" },
+                { 5, "foo\r\n" },
+                { 5, "foo\n\r" },
+                { 6, "foo\r\n\0" },
+                { 6, "foo\n\r\0" },
+        };
+
+        size_t i;
+        int r;
+
+        for (i = 0; i < ELEMENTSOF(eof_endings); i++) {
+                _cleanup_fclose_ FILE *f = NULL;
+                _cleanup_free_ char *s = NULL;
+
+                assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r"));
+
+                r = read_line(f, (size_t) -1, &s);
+                assert_se((size_t) r == eof_endings[i].length);
+                assert_se(streq_ptr(s, "foo"));
+
+                assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */
+        }
+}
+
 int main(int argc, char *argv[]) {
         test_setup_logging(LOG_DEBUG);
 
@@ -735,6 +770,7 @@ int main(int argc, char *argv[]) {
         test_read_line();
         test_read_line2();
         test_read_line3();
+        test_read_line4();
 
         return 0;
 }