LCOV - code coverage report
Current view: top level - tests/gpgsm - t-support.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 37 64.9 %
Date: 2018-11-14 16:53:58 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* t-support.h - Helper routines for regression tests.
       2             :    Copyright (C) 2000 Werner Koch (dd9jn)
       3             :    Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
       4             : 
       5             :    This file is part of GPGME.
       6             : 
       7             :    GPGME is free software; you can redistribute it and/or modify it
       8             :    under the terms of the GNU Lesser General Public License as
       9             :    published by the Free Software Foundation; either version 2.1 of
      10             :    the License, or (at your option) any later version.
      11             : 
      12             :    GPGME is distributed in the hope that it will be useful, but
      13             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :    Lesser General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU Lesser General Public
      18             :    License along with this program; if not, write to the Free Software
      19             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      20             :    02111-1307, USA.  */
      21             : 
      22             : #include <unistd.h>
      23             : #include <errno.h>
      24             : #include <stdlib.h>
      25             : #include <locale.h>
      26             : 
      27             : #include <gpgme.h>
      28             : 
      29             : #define fail_if_err(err)                                        \
      30             :   do                                                            \
      31             :     {                                                           \
      32             :       if (err)                                                  \
      33             :         {                                                       \
      34             :           fprintf (stderr, "%s:%d: %s: %s (%d.%d)\n",         \
      35             :                    __FILE__, __LINE__, gpgme_strsource (err),   \
      36             :                    gpgme_strerror (err),                        \
      37             :                    gpgme_err_source (err), gpgme_err_code (err)); \
      38             :           exit (1);                                             \
      39             :         }                                                       \
      40             :     }                                                           \
      41             :   while (0)
      42             : 
      43             : 
      44             : void
      45           7 : print_data (gpgme_data_t dh)
      46             : {
      47             : #define BUF_SIZE 512
      48             :   char buf[BUF_SIZE + 1];
      49             :   int ret;
      50             : 
      51           7 :   ret = gpgme_data_seek (dh, 0, SEEK_SET);
      52           7 :   if (ret)
      53           0 :     fail_if_err (gpgme_error_from_errno (errno));
      54          35 :   while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
      55          21 :     fwrite (buf, ret, 1, stdout);
      56           7 :   if (ret < 0)
      57           0 :     fail_if_err (gpgme_error_from_errno (errno));
      58           7 : }
      59             : 
      60             : 
      61             : gpgme_error_t
      62           0 : passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info,
      63             :                int last_was_bad, int fd)
      64             : {
      65             :   int res;
      66           0 :   char pass[] = "abc\n";
      67           0 :   int passlen = strlen (pass);
      68           0 :   int off = 0;
      69             : 
      70             :   (void)opaque;
      71             :   (void)uid_hint;
      72             :   (void)passphrase_info;
      73             :   (void)last_was_bad;
      74             : 
      75             :   do
      76             :     {
      77           0 :       res = gpgme_io_write (fd, &pass[off], passlen - off);
      78           0 :       if (res > 0)
      79           0 :         off += res;
      80             :     }
      81           0 :   while (res > 0 && off != passlen);
      82             : 
      83           0 :   return off == passlen ? 0 : gpgme_error_from_errno (errno);
      84             : }
      85             : 
      86             : 
      87             : char *
      88           2 : make_filename (const char *fname)
      89             : {
      90           2 :   const char *srcdir = getenv ("srcdir");
      91             :   char *buf;
      92             : 
      93           2 :   if (!srcdir)
      94           0 :     srcdir = ".";
      95           2 :   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
      96           2 :   if (!buf)
      97           0 :     exit (8);
      98           2 :   strcpy (buf, srcdir);
      99           2 :   strcat (buf, "/");
     100           2 :   strcat (buf, fname);
     101           2 :   return buf;
     102             : }
     103             : 
     104             : 
     105             : void
     106           7 : init_gpgme (gpgme_protocol_t proto)
     107             : {
     108             :   gpgme_error_t err;
     109             : 
     110           7 :   gpgme_check_version (NULL);
     111             : #ifndef HAVE_W32_SYSTEM
     112           7 :   setlocale (LC_ALL, "");
     113           7 :   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
     114           7 :   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
     115             : #endif
     116             : 
     117           7 :   err = gpgme_engine_check_version (proto);
     118           7 :   fail_if_err (err);
     119           7 : }

Generated by: LCOV version 1.13