summaryrefslogtreecommitdiffstats
path: root/test/utils.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-29 04:40:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-29 04:40:20 +0000
commit2dba2525fb35dcfc79aad5bdf6c92e790d69635c (patch)
treeac8ac7d3960922094733bac6d9a5300da7171c56 /test/utils.c
parentInitial commit. (diff)
downloadlibnvme-2dba2525fb35dcfc79aad5bdf6c92e790d69635c.tar.xz
libnvme-2dba2525fb35dcfc79aad5bdf6c92e790d69635c.zip
Adding upstream version 1.3.upstream/1.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/utils.c')
-rw-r--r--test/utils.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/test/utils.c b/test/utils.c
new file mode 100644
index 0000000..60665b8
--- /dev/null
+++ b/test/utils.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * This file is part of libnvme.
+ *
+ * Common test utilities.
+ *
+ * Copyright (c) 2022 Code Construct
+ */
+
+#include <err.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "utils.h"
+
+FILE *test_setup_log(void)
+{
+ FILE *fd;
+
+ fd = tmpfile();
+ if (!fd)
+ err(EXIT_FAILURE, "can't create temporary file for log buf");
+
+ return fd;
+}
+
+void test_close_log(FILE *fd)
+{
+ fclose(fd);
+}
+
+void test_print_log_buf(FILE *logfd)
+{
+ char buf[4096];
+ int rc;
+
+ if (!ftell(logfd))
+ return;
+
+ rewind(logfd);
+
+ printf("--- begin test output\n");
+
+ while (!feof(logfd) && !ferror(logfd)) {
+ size_t rlen, wlen, wpos;
+
+ rlen = fread(buf, 1, sizeof(buf), logfd);
+ if (rlen <= 0)
+ break;
+
+ for (wpos = 0; wpos < rlen;) {
+ wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout);
+ if (wlen == 0)
+ break;
+ wpos += wlen;
+ }
+
+ if (feof(logfd) || ferror((logfd)))
+ break;
+ }
+
+ printf("--- end test output\n");
+ rewind(logfd);
+ rc = ftruncate(fileno(logfd), 0);
+ if (rc)
+ printf("failed to truncate log buf; further output may be invalid\n");
+}
+