| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef _GNU_SOURCE |
| 25 |
# define _GNU_SOURCE |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <errno.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#include "common.h" |
| 35 |
#include "progname.h" |
| 36 |
#include "system.h" |
| 37 |
#include "testutils.h" |
| 38 |
#include "xalloc.h" |
| 39 |
|
| 40 |
static size_t test_counter; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static bool |
| 45 |
test_file_is_executable (const char *file) |
| 46 |
{ |
| 47 |
struct stat sb; |
| 48 |
|
| 49 |
if (stat (file, &sb) < 0) |
| 50 |
return false; |
| 51 |
|
| 52 |
if (S_ISREG (sb.st_mode) && (sb.st_mode & 0111) != 0) |
| 53 |
return true; |
| 54 |
|
| 55 |
errno = S_ISDIR (sb.st_mode) ? EISDIR : EACCES; |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
static int |
| 60 |
test_use_terminal_colors (void) |
| 61 |
{ |
| 62 |
return isatty (STDIN_FILENO); |
| 63 |
} |
| 64 |
|
| 65 |
char * |
| 66 |
test_fstringify (const char * filename) |
| 67 |
{ |
| 68 |
char * buffer = NULL; |
| 69 |
long size; |
| 70 |
size_t chars; |
| 71 |
FILE * stream = fopen (filename, "r"); |
| 72 |
|
| 73 |
if (NULL == stream) |
| 74 |
return NULL; |
| 75 |
|
| 76 |
fseek (stream, 0, SEEK_END); |
| 77 |
if ((size = ftell (stream)) < 0) |
| 78 |
{ |
| 79 |
fclose (stream); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
rewind (stream); |
| 84 |
|
| 85 |
buffer = xmalloc ((size_t)size + 1); |
| 86 |
chars = fread (buffer, 1, (size_t)size, stream); |
| 87 |
|
| 88 |
if (ferror (stream) || (chars < (size_t)size)) |
| 89 |
{ |
| 90 |
free (buffer); |
| 91 |
buffer = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
fclose (stream); |
| 95 |
return buffer; |
| 96 |
} |
| 97 |
|
| 98 |
int |
| 99 |
test_main (int argc, char **argv, int (*func) (void), ...) |
| 100 |
{ |
| 101 |
const char *lib; |
| 102 |
va_list ap; |
| 103 |
int ret; |
| 104 |
|
| 105 |
va_start (ap, func); |
| 106 |
while ((lib = va_arg(ap, const char *))) |
| 107 |
TEST_PRELOAD (lib); |
| 108 |
va_end(ap); |
| 109 |
|
| 110 |
program_name = argv[0]; |
| 111 |
ret = (func) (); |
| 112 |
|
| 113 |
return ret; |
| 114 |
} |
| 115 |
|
| 116 |
int |
| 117 |
test_run (const char *title, int (*body) (const void *data), const void *data) |
| 118 |
{ |
| 119 |
int ret = body (data); |
| 120 |
test_counter++; |
| 121 |
|
| 122 |
fprintf (stderr, "%2zu) %-65s ... ", test_counter, title); |
| 123 |
|
| 124 |
if (test_use_terminal_colors ()) |
| 125 |
fprintf (stderr, "%s\n", |
| 126 |
(ret == 0) ? "\e[32mOK\e[0m" : |
| 127 |
(ret == EXIT_AM_SKIP) ? "\e[34m\e[1mSKIP\e[0m" : |
| 128 |
(ret == EXIT_AM_HARDFAIL) ? |
| 129 |
"\e[31m\e[1mHARDFAIL\e[0m" : "\e[31m\e[1mFAILED\e[0m"); |
| 130 |
else |
| 131 |
fprintf (stderr, "%s\n", |
| 132 |
(ret == 0) ? "OK" : |
| 133 |
(ret == EXIT_AM_SKIP) ? "SKIP" : |
| 134 |
(ret == EXIT_AM_HARDFAIL) ? "HARDFAIL" : "FAILED"); |
| 135 |
|
| 136 |
return ret; |
| 137 |
} |