summaryrefslogtreecommitdiff
path: root/src/test/test-fileio.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-12-17 11:52:51 +0100
committerLennart Poettering <lennart@poettering.net>2018-12-18 15:03:22 +0100
commit3946d5762f801c037eade25873eb10ab946d56cd (patch)
tree7be330d1a1dfbf97142d48bca2cf2c4440feb428 /src/test/test-fileio.c
parent91a306b81322dae9c6d99421a2f38d763c518d2e (diff)
downloadsystemd-3946d5762f801c037eade25873eb10ab946d56cd.tar.gz
test: add test case for read_nul_string()
Diffstat (limited to 'src/test/test-fileio.c')
-rw-r--r--src/test/test-fileio.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index fd0cc27de8..b3f1f0aabb 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -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;
}