]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add test case for read_nul_string()
authorLennart Poettering <lennart@poettering.net>
Mon, 17 Dec 2018 10:52:51 +0000 (11:52 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 18 Dec 2018 14:03:22 +0000 (15:03 +0100)
src/test/test-fileio.c

index fd0cc27de83dcaae885d3641c384d5c93f9f51d6..b3f1f0aabba494f38bc07a7c97bd3fc2a571b564 100644 (file)
@@ -748,6 +748,40 @@ static void test_read_line4(void) {
         }
 }
 
+static void test_read_nul_string(void) {
+        static const char test[] = "string nr. 1\0"
+                "string nr. 2\n\0"
+                "empty string follows\0"
+                "\0"
+                "final string\n is empty\0"
+                "\0";
+
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *s = NULL;
+
+        assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r"));
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
+        s = mfree(s);
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n"));
+        s = mfree(s);
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 21 && streq_ptr(s, "empty string follows"));
+        s = mfree(s);
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
+        s = mfree(s);
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty"));
+        s = mfree(s);
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
+        s = mfree(s);
+
+        assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, ""));
+}
+
 int main(int argc, char *argv[]) {
         test_setup_logging(LOG_DEBUG);
 
@@ -771,6 +805,7 @@ int main(int argc, char *argv[]) {
         test_read_line2();
         test_read_line3();
         test_read_line4();
+        test_read_nul_string();
 
         return 0;
 }