From: Lennart Poettering Date: Mon, 17 Dec 2018 10:52:51 +0000 (+0100) Subject: test: add test case for read_nul_string() X-Git-Tag: v240~23^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3946d5762f801c037eade25873eb10ab946d56cd;p=platform%2Fupstream%2Fsystemd.git test: add test case for read_nul_string() --- diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index fd0cc27..b3f1f0a 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; }