diff options
author | 2018-12-17 11:52:51 +0100 | |
---|---|---|
committer | 2018-12-18 15:03:22 +0100 | |
commit | 3946d5762f801c037eade25873eb10ab946d56cd (patch) | |
tree | 7be330d1a1dfbf97142d48bca2cf2c4440feb428 | |
parent | fileio: let's minimize 'count' inc/dec calls (diff) | |
download | systemd-3946d5762f801c037eade25873eb10ab946d56cd.tar.gz systemd-3946d5762f801c037eade25873eb10ab946d56cd.tar.bz2 systemd-3946d5762f801c037eade25873eb10ab946d56cd.zip |
test: add test case for read_nul_string()
-rw-r--r-- | src/test/test-fileio.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index fd0cc27de..b3f1f0aab 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; } |