| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* |
| 3 |
* This file is part of libnvme. |
| 4 |
* |
| 5 |
* Common test utilities. |
| 6 |
* |
| 7 |
* Copyright (c) 2022 Code Construct |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <err.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include "utils.h" |
| 15 |
|
| 16 |
FILE *test_setup_log(void) |
| 17 |
{ |
| 18 |
FILE *fd; |
| 19 |
|
| 20 |
fd = tmpfile(); |
| 21 |
if (!fd) |
| 22 |
err(EXIT_FAILURE, "can't create temporary file for log buf"); |
| 23 |
|
| 24 |
return fd; |
| 25 |
} |
| 26 |
|
| 27 |
void test_close_log(FILE *fd) |
| 28 |
{ |
| 29 |
fclose(fd); |
| 30 |
} |
| 31 |
|
| 32 |
void test_print_log_buf(FILE *logfd) |
| 33 |
{ |
| 34 |
char buf[4096]; |
| 35 |
int rc; |
| 36 |
|
| 37 |
if (!ftell(logfd)) |
| 38 |
return; |
| 39 |
|
| 40 |
rewind(logfd); |
| 41 |
|
| 42 |
printf("--- begin test output\n"); |
| 43 |
|
| 44 |
while (!feof(logfd) && !ferror(logfd)) { |
| 45 |
size_t rlen, wlen, wpos; |
| 46 |
|
| 47 |
rlen = fread(buf, 1, sizeof(buf), logfd); |
| 48 |
if (rlen <= 0) |
| 49 |
break; |
| 50 |
|
| 51 |
for (wpos = 0; wpos < rlen;) { |
| 52 |
wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout); |
| 53 |
if (wlen == 0) |
| 54 |
break; |
| 55 |
wpos += wlen; |
| 56 |
} |
| 57 |
|
| 58 |
if (feof(logfd) || ferror((logfd))) |
| 59 |
break; |
| 60 |
} |
| 61 |
|
| 62 |
printf("--- end test output\n"); |
| 63 |
rewind(logfd); |
| 64 |
rc = ftruncate(fileno(logfd), 0); |
| 65 |
if (rc) |
| 66 |
printf("failed to truncate log buf; further output may be invalid\n"); |
| 67 |
} |
| 68 |
|