| 1 |
/* t-common.h - Common code for the tests. |
| 2 |
* Copyright (C) 2013 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of libgpg-error. |
| 5 |
* |
| 6 |
* libgpg-error is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* libgpg-error is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdarg.h> |
| 21 |
|
| 22 |
#include "../src/gpgrt.h" |
| 23 |
|
| 24 |
#ifndef PGM |
| 25 |
# error Macro PGM not defined. |
| 26 |
#endif |
| 27 |
#ifndef DIM |
| 28 |
# define DIM(array) (sizeof (array) / sizeof (*array)) |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
static int verbose; |
| 33 |
static int debug; |
| 34 |
static int errorcount; |
| 35 |
static const char *current_func; |
| 36 |
|
| 37 |
static void die (const char *format, ...) GPGRT_ATTR_NR_PRINTF(1,2); |
| 38 |
static void fail (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); |
| 39 |
static void show (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); |
| 40 |
|
| 41 |
|
| 42 |
/* Set the name of the function to be used in error messages. */ |
| 43 |
#define enter_test_function() do { current_func = __func__; } while(0) |
| 44 |
#define leave_test_function() do { current_func = NULL; } while(0) |
| 45 |
|
| 46 |
|
| 47 |
static void * |
| 48 |
xmalloc (size_t n) |
| 49 |
{ |
| 50 |
char *p = gpgrt_malloc (n); |
| 51 |
if (!p) |
| 52 |
die ("out of core\n"); |
| 53 |
return p; |
| 54 |
} |
| 55 |
|
| 56 |
static char * |
| 57 |
xstrdup (const char *s) |
| 58 |
{ |
| 59 |
char *p = gpgrt_strdup (s); |
| 60 |
if (!p) |
| 61 |
die ("out of core\n"); |
| 62 |
return p; |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
xfree (void *p) |
| 67 |
{ |
| 68 |
if (p) |
| 69 |
gpgrt_free (p); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static void |
| 75 |
die (const char *format, ...) |
| 76 |
{ |
| 77 |
va_list arg_ptr ; |
| 78 |
|
| 79 |
fflush (stdout); |
| 80 |
#ifdef HAVE_FLOCKFILE |
| 81 |
flockfile (stderr); |
| 82 |
#endif |
| 83 |
if (current_func) |
| 84 |
fprintf (stderr, "%s:%s: ", PGM, current_func); |
| 85 |
else |
| 86 |
fprintf (stderr, "%s: ", PGM); |
| 87 |
va_start (arg_ptr, format) ; |
| 88 |
vfprintf (stderr, format, arg_ptr); |
| 89 |
va_end (arg_ptr); |
| 90 |
if (*format && format[strlen(format)-1] != '\n') |
| 91 |
putc ('\n', stderr); |
| 92 |
#ifdef HAVE_FLOCKFILE |
| 93 |
funlockfile (stderr); |
| 94 |
#endif |
| 95 |
xfree (xstrdup ("")); /* To avoid compiler warnings. */ |
| 96 |
xfree (xmalloc (16)); /* To avoid compiler warnings. */ |
| 97 |
exit (1); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
static void |
| 102 |
fail (const char *format, ...) |
| 103 |
{ |
| 104 |
va_list arg_ptr; |
| 105 |
|
| 106 |
fflush (stdout); |
| 107 |
#ifdef HAVE_FLOCKFILE |
| 108 |
flockfile (stderr); |
| 109 |
#endif |
| 110 |
if (current_func) |
| 111 |
fprintf (stderr, "%s:%s: ", PGM, current_func); |
| 112 |
else |
| 113 |
fprintf (stderr, "%s: ", PGM); |
| 114 |
va_start (arg_ptr, format); |
| 115 |
vfprintf (stderr, format, arg_ptr); |
| 116 |
va_end (arg_ptr); |
| 117 |
if (*format && format[strlen(format)-1] != '\n') |
| 118 |
putc ('\n', stderr); |
| 119 |
#ifdef HAVE_FLOCKFILE |
| 120 |
funlockfile (stderr); |
| 121 |
#endif |
| 122 |
errorcount++; |
| 123 |
if (errorcount >= 50) |
| 124 |
die ("stopped after 50 errors."); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
static void |
| 129 |
show (const char *format, ...) |
| 130 |
{ |
| 131 |
va_list arg_ptr; |
| 132 |
|
| 133 |
if (!verbose) |
| 134 |
return; |
| 135 |
#ifdef HAVE_FLOCKFILE |
| 136 |
flockfile (stderr); |
| 137 |
#endif |
| 138 |
if (current_func) |
| 139 |
fprintf (stderr, "%s:%s: ", PGM, current_func); |
| 140 |
else |
| 141 |
fprintf (stderr, "%s: ", PGM); |
| 142 |
va_start (arg_ptr, format); |
| 143 |
vfprintf (stderr, format, arg_ptr); |
| 144 |
if (*format && format[strlen(format)-1] != '\n') |
| 145 |
putc ('\n', stderr); |
| 146 |
va_end (arg_ptr); |
| 147 |
#ifdef HAVE_FLOCKFILE |
| 148 |
funlockfile (stderr); |
| 149 |
#endif |
| 150 |
} |