diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-05-26 08:22:03 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-05-31 23:48:43 +0200 |
commit | abe72100cfc292093153d48a132a5ab1b5f61dd5 (patch) | |
tree | 335ff28128c4ebce11c04d4691dd24695d6600ca /src/test/test-memstream-util.c | |
parent | calendarspec: rename arguments (diff) | |
download | systemd-abe72100cfc292093153d48a132a5ab1b5f61dd5.tar.xz systemd-abe72100cfc292093153d48a132a5ab1b5f61dd5.zip |
util: introduce memstream-util
There is many pitfalls in using memstream.
Let's introduce a wrapper to make us safely use it.
Diffstat (limited to '')
-rw-r--r-- | src/test/test-memstream-util.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/test/test-memstream-util.c b/src/test/test-memstream-util.c new file mode 100644 index 0000000000..254bdcaa15 --- /dev/null +++ b/src/test/test-memstream-util.c @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "memstream-util.h" +#include "string-util.h" +#include "tests.h" + +TEST(memstream_done) { + _cleanup_(memstream_done) MemStream m = {}; + + assert_se(memstream_init(&m)); +} + +TEST(memstream_empty) { + _cleanup_(memstream_done) MemStream m = {}; + _cleanup_free_ char *buf = NULL; + size_t sz; + + assert_se(memstream_init(&m)); + assert_se(memstream_finalize(&m, &buf, &sz) >= 0); + assert_se(streq(buf, "")); + assert_se(sz == 0); +} + +TEST(memstream) { + _cleanup_(memstream_done) MemStream m = {}; + _cleanup_free_ char *buf = NULL; + size_t sz; + FILE *f; + + assert_se(f = memstream_init(&m)); + fputs("hoge", f); + fputs("おはよう!", f); + fputs(u8"😀😀😀", f); + assert_se(memstream_finalize(&m, &buf, &sz) >= 0); + assert_se(streq(buf, u8"hogeおはよう!😀😀😀")); + assert_se(sz == strlen(u8"hogeおはよう!😀😀😀")); + + buf = mfree(buf); + + assert_se(f = memstream_init(&m)); + fputs("second", f); + assert_se(memstream_finalize(&m, &buf, &sz) >= 0); + assert_se(streq(buf, "second")); + assert_se(sz == strlen("second")); +} + +TEST(memstream_dump) { + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; + + assert_se(f = memstream_init(&m)); + fputs("first", f); + assert_se(memstream_dump(LOG_DEBUG, &m) >= 0); + + assert_se(f = memstream_init(&m)); + fputs("second", f); + assert_se(memstream_dump(LOG_DEBUG, &m) >= 0); +} + +DEFINE_TEST_MAIN(LOG_DEBUG); |