LCOV - code coverage report
Current view: top level - tests - t-common.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 44 34.1 %
Date: 2016-12-15 12:59:22 Functions: 2 3 66.7 %

          Line data    Source code
       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 <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <stdarg.h>
      21             : 
      22             : #include "../src/gcrypt.h"
      23             : 
      24             : #ifndef PGM
      25             : # error Macro PGM not defined.
      26             : #endif
      27             : #ifndef _GCRYPT_CONFIG_H_INCLUDED
      28             : # error config.h not included
      29             : #endif
      30             : 
      31             : /* A couple of useful macros.  */
      32             : #ifndef DIM
      33             : # define DIM(v)              (sizeof(v)/sizeof((v)[0]))
      34             : #endif
      35             : #define DIMof(type,member)   DIM(((type *)0)->member)
      36             : #define xmalloc(a)    gcry_xmalloc ((a))
      37             : #define xcalloc(a,b)  gcry_xcalloc ((a),(b))
      38             : #define xstrdup(a)    gcry_xstrdup ((a))
      39             : #define xfree(a)      gcry_free ((a))
      40             : #define my_isascii(c) (!((c) & 0x80))
      41             : #define digitp(p)     (*(p) >= '0' && *(p) <= '9')
      42             : #define hexdigitp(a)  (digitp (a)                     \
      43             :                        || (*(a) >= 'A' && *(a) <= 'F')  \
      44             :                        || (*(a) >= 'a' && *(a) <= 'f'))
      45             : #define xtoi_1(p)     (*(p) <= '9'? (*(p)- '0'): \
      46             :                        *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
      47             : #define xtoi_2(p)     ((xtoi_1(p) * 16) + xtoi_1((p)+1))
      48             : #define xmalloc(a)    gcry_xmalloc ((a))
      49             : #define xcalloc(a,b)  gcry_xcalloc ((a),(b))
      50             : #define xstrdup(a)    gcry_xstrdup ((a))
      51             : #define xfree(a)      gcry_free ((a))
      52             : #define pass()        do { ; } while (0)
      53             : 
      54             : 
      55             : /* Standard global variables.  */
      56             : static const char *wherestr;
      57             : static int verbose;
      58             : static int debug;
      59             : static int error_count;
      60             : static int die_on_error;
      61             : 
      62             : /* If we have a decent libgpg-error we can use some gcc attributes.  */
      63             : #ifdef GPGRT_ATTR_NORETURN
      64             : static void die (const char *format, ...)
      65             :   GPGRT_ATTR_UNUSED GPGRT_ATTR_NR_PRINTF(1,2);
      66             : static void fail (const char *format, ...)
      67             :   GPGRT_ATTR_UNUSED GPGRT_ATTR_PRINTF(1,2);
      68             : static void info (const char *format, ...) \
      69             :   GPGRT_ATTR_UNUSED GPGRT_ATTR_PRINTF(1,2);
      70             : #endif /*GPGRT_ATTR_NORETURN*/
      71             : 
      72             : 
      73             : /* Reporting functions.  */
      74             : static void
      75           6 : die (const char *format, ...)
      76             : {
      77             :   va_list arg_ptr ;
      78             : 
      79             :   /* Avoid warning.  */
      80             :   (void) debug;
      81             : 
      82           6 :   fflush (stdout);
      83             : #ifdef HAVE_FLOCKFILE
      84           6 :   flockfile (stderr);
      85             : #endif
      86           6 :   fprintf (stderr, "%s: ", PGM);
      87           6 :   if (wherestr)
      88           0 :     fprintf (stderr, "%s: ", wherestr);
      89           6 :   va_start (arg_ptr, format) ;
      90           6 :   vfprintf (stderr, format, arg_ptr);
      91           6 :   va_end (arg_ptr);
      92           6 :   if (*format && format[strlen(format)-1] != '\n')
      93           3 :     putc ('\n', stderr);
      94             : #ifdef HAVE_FLOCKFILE
      95           6 :   funlockfile (stderr);
      96             : #endif
      97           6 :   exit (1);
      98             : }
      99             : 
     100             : 
     101             : static void
     102           0 : fail (const char *format, ...)
     103             : {
     104             :   va_list arg_ptr;
     105             : 
     106           0 :   fflush (stdout);
     107             : #ifdef HAVE_FLOCKFILE
     108           0 :   flockfile (stderr);
     109             : #endif
     110           0 :   fprintf (stderr, "%s: ", PGM);
     111           0 :   if (wherestr)
     112           0 :     fprintf (stderr, "%s: ", wherestr);
     113           0 :   va_start (arg_ptr, format);
     114           0 :   vfprintf (stderr, format, arg_ptr);
     115           0 :   va_end (arg_ptr);
     116           0 :   if (*format && format[strlen(format)-1] != '\n')
     117           0 :     putc ('\n', stderr);
     118             : #ifdef HAVE_FLOCKFILE
     119           0 :   funlockfile (stderr);
     120             : #endif
     121           0 :   if (die_on_error)
     122           0 :     exit (1);
     123           0 :   error_count++;
     124           0 :   if (error_count >= 50)
     125           0 :     die ("stopped after 50 errors.");
     126           0 : }
     127             : 
     128             : 
     129             : static void
     130          29 : info (const char *format, ...)
     131             : {
     132             :   va_list arg_ptr;
     133             : 
     134          29 :   if (!verbose)
     135          29 :     return;
     136             : #ifdef HAVE_FLOCKFILE
     137           0 :   flockfile (stderr);
     138             : #endif
     139           0 :   fprintf (stderr, "%s: ", PGM);
     140           0 :   if (wherestr)
     141           0 :     fprintf (stderr, "%s: ", wherestr);
     142           0 :   va_start (arg_ptr, format);
     143           0 :   vfprintf (stderr, format, arg_ptr);
     144           0 :   if (*format && format[strlen(format)-1] != '\n')
     145           0 :     putc ('\n', stderr);
     146           0 :   va_end (arg_ptr);
     147             : #ifdef HAVE_FLOCKFILE
     148           0 :   funlockfile (stderr);
     149             : #endif
     150             : }
     151             : 
     152             : 
     153             : /* Convenience macro for initializing gcrypt with error checking.  */
     154             : #define xgcry_control(cmd...)                                   \
     155             :   do {                                                          \
     156             :     gcry_error_t err__ = gcry_control (cmd);                    \
     157             :     if (err__)                                                  \
     158             :       die ("line %d: gcry_control (%s) failed: %s",             \
     159             :            __LINE__, #cmd, gcry_strerror (err__));              \
     160             :   } while (0)

Generated by: LCOV version 1.12