summaryrefslogtreecommitdiff
path: root/src/journal/test-catalog.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-05-16 11:35:41 +0200
committerLennart Poettering <lennart@poettering.net>2018-05-18 20:11:31 +0200
commit627d2bac2477986401400127fb31af33ca0f69b0 (patch)
treec2e0593b9a4c2c895fe2dcaa03e240e0513bfbee /src/journal/test-catalog.c
parentc55b280158a5a349adb091bf6c3600d4cf7f8020 (diff)
downloadsystemd-627d2bac2477986401400127fb31af33ca0f69b0.tar.gz
fs-util,test: add helper to remove tempfiles
This simplifies the use of tempfiles in tests and fixes "leaked" temporary files in test-fileio, test-catalog, test-conf-parser. Not the whole tree is converted.
Diffstat (limited to 'src/journal/test-catalog.c')
-rw-r--r--src/journal/test-catalog.c29
1 files changed, 9 insertions, 20 deletions
diff --git a/src/journal/test-catalog.c b/src/journal/test-catalog.c
index d5640c549c..67466cf326 100644
--- a/src/journal/test-catalog.c
+++ b/src/journal/test-catalog.c
@@ -16,6 +16,7 @@
#include "alloc-util.h"
#include "catalog.h"
#include "fd-util.h"
+#include "fs-util.h"
#include "fileio.h"
#include "log.h"
#include "macro.h"
@@ -32,9 +33,8 @@ static const char *no_catalog_dirs[] = {
NULL
};
-static Hashmap * test_import(const char* contents, ssize_t size, int code) {
- int r;
- char name[] = "/tmp/test-catalog.XXXXXX";
+static Hashmap* test_import(const char* contents, ssize_t size, int code) {
+ _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-catalog.XXXXXX";
_cleanup_close_ int fd;
Hashmap *h;
@@ -47,10 +47,7 @@ static Hashmap * test_import(const char* contents, ssize_t size, int code) {
assert_se(fd >= 0);
assert_se(write(fd, contents, size) == size);
- r = catalog_import_file(h, name);
- assert_se(r == code);
-
- unlink(name);
+ assert_se(catalog_import_file(h, name) == code);
return h;
}
@@ -164,17 +161,9 @@ static void test_catalog_import_merge_no_body(void) {
}
}
-static const char* database = NULL;
-
-static void test_catalog_update(void) {
- static char name[] = "/tmp/test-catalog.XXXXXX";
+static void test_catalog_update(const char *database) {
int r;
- r = mkostemp_safe(name);
- assert_se(r >= 0);
-
- database = name;
-
/* Test what happens if there are no files. */
r = catalog_update(database, NULL, NULL);
assert_se(r == 0);
@@ -218,6 +207,7 @@ static void test_catalog_file_lang(void) {
}
int main(int argc, char *argv[]) {
+ _cleanup_(unlink_tempfilep) char database[] = "/tmp/test-catalog.XXXXXX";
_cleanup_free_ char *text = NULL;
int r;
@@ -234,7 +224,9 @@ int main(int argc, char *argv[]) {
test_catalog_import_merge();
test_catalog_import_merge_no_body();
- test_catalog_update();
+ assert_se(mkostemp_safe(database) >= 0);
+
+ test_catalog_update(database);
r = catalog_list(stdout, database, true);
assert_se(r >= 0);
@@ -245,8 +237,5 @@ int main(int argc, char *argv[]) {
assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
printf(">>>%s<<<\n", text);
- if (database)
- unlink(database);
-
return 0;
}