LCOV - code coverage report
Current view: top level - tests - run-support.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 60 25.0 %
Date: 2018-11-15 08:49:49 Functions: 2 7 28.6 %

          Line data    Source code
       1             : /* run-support.h - Helper routines for run-* test programs.
       2             :    Copyright (C) 2000 Werner Koch (dd9jn)
       3             :    Copyright (C) 2001, 2002, 2003, 2004, 2009 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, see <https://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include <unistd.h>
      22             : #include <errno.h>
      23             : #include <stdlib.h>
      24             : #include <locale.h>
      25             : 
      26             : #ifdef HAVE_W32_SYSTEM
      27             : #include <windows.h>
      28             : #endif
      29             : 
      30             : #include <gpgme.h>
      31             : 
      32             : #ifndef DIM
      33             : #define DIM(v)               (sizeof(v)/sizeof((v)[0]))
      34             : #endif
      35             : 
      36             : #define fail_if_err(err)                                        \
      37             :   do                                                            \
      38             :     {                                                           \
      39             :       if (err)                                                  \
      40             :         {                                                       \
      41             :           fprintf (stderr, PGM": file %s line %d: <%s> %s\n",   \
      42             :                    __FILE__, __LINE__, gpgme_strsource (err),   \
      43             :                    gpgme_strerror (err));                       \
      44             :           exit (1);                                             \
      45             :         }                                                       \
      46             :     }                                                           \
      47             :   while (0)
      48             : 
      49             : 
      50             : static const char *
      51           0 : nonnull (const char *s)
      52             : {
      53           0 :   return s? s :"[none]";
      54             : }
      55             : 
      56             : 
      57             : void
      58           0 : print_data (gpgme_data_t dh)
      59             : {
      60             : #define BUF_SIZE 512
      61             :   char buf[BUF_SIZE + 1];
      62             :   int ret;
      63             : 
      64           0 :   ret = gpgme_data_seek (dh, 0, SEEK_SET);
      65           0 :   if (ret)
      66           0 :     fail_if_err (gpgme_err_code_from_errno (errno));
      67           0 :   while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
      68           0 :     fwrite (buf, ret, 1, stdout);
      69           0 :   if (ret < 0)
      70           0 :     fail_if_err (gpgme_err_code_from_errno (errno));
      71           0 : }
      72             : 
      73             : 
      74             : gpgme_error_t
      75           0 : passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info,
      76             :                int last_was_bad, int fd)
      77             : {
      78             :   int res;
      79           0 :   char pass[] = "abc\n";
      80           0 :   int passlen = strlen (pass);
      81           0 :   int off = 0;
      82             : 
      83             :   (void)opaque;
      84             :   (void)uid_hint;
      85             :   (void)passphrase_info;
      86             :   (void)last_was_bad;
      87             : 
      88             :   do
      89             :     {
      90           0 :       res = gpgme_io_write (fd, &pass[off], passlen - off);
      91           0 :       if (res > 0)
      92           0 :         off += res;
      93             :     }
      94           0 :   while (res > 0 && off != passlen);
      95             : 
      96           0 :   return off == passlen ? 0 : gpgme_error_from_errno (errno);
      97             : }
      98             : 
      99             : 
     100             : char *
     101           2 : make_filename (const char *fname)
     102             : {
     103           2 :   const char *srcdir = getenv ("srcdir");
     104             :   char *buf;
     105             : 
     106           2 :   if (!srcdir)
     107           0 :     srcdir = ".";
     108           2 :   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
     109           2 :   if (!buf)
     110             :     {
     111           0 :       fprintf (stderr, "%s:%d: could not allocate string: %s\n",
     112           0 :                __FILE__, __LINE__, strerror (errno));
     113           0 :       exit (8);
     114             :     }
     115           2 :   strcpy (buf, srcdir);
     116           2 :   strcat (buf, "/");
     117           2 :   strcat (buf, fname);
     118           2 :   return buf;
     119             : }
     120             : 
     121             : 
     122             : void
     123           1 : init_gpgme_basic (void)
     124             : {
     125           1 :   gpgme_check_version (NULL);
     126           1 :   setlocale (LC_ALL, "");
     127           1 :   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
     128             : #ifndef HAVE_W32_SYSTEM
     129           1 :   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
     130             : #endif
     131           1 : }
     132             : 
     133             : 
     134             : void
     135           0 : init_gpgme (gpgme_protocol_t proto)
     136             : {
     137             :   gpg_error_t err;
     138             : 
     139           0 :   init_gpgme_basic ();
     140           0 :   err = gpgme_engine_check_version (proto);
     141           0 :   fail_if_err (err);
     142           0 : }
     143             : 
     144             : 
     145             : void
     146           0 : print_import_result (gpgme_import_result_t r)
     147             : {
     148             :   gpgme_import_status_t st;
     149             : 
     150           0 :   for (st=r->imports; st; st = st->next)
     151             :     {
     152           0 :       printf ("  fpr: %s err: %d (%s) status:", nonnull (st->fpr),
     153             :               st->result, gpgme_strerror (st->result));
     154           0 :       if (st->status & GPGME_IMPORT_NEW)
     155           0 :         fputs (" new", stdout);
     156           0 :       if (st->status & GPGME_IMPORT_UID)
     157           0 :         fputs (" uid", stdout);
     158           0 :       if (st->status & GPGME_IMPORT_SIG)
     159           0 :         fputs (" sig", stdout);
     160           0 :       if (st->status & GPGME_IMPORT_SUBKEY)
     161           0 :         fputs (" subkey", stdout);
     162           0 :       if (st->status & GPGME_IMPORT_SECRET)
     163           0 :         fputs (" secret", stdout);
     164           0 :       putchar ('\n');
     165             :     }
     166           0 :   printf ("key import summary:\n"
     167             :           "        considered: %d\n"
     168             :           "        no user id: %d\n"
     169             :           "          imported: %d\n"
     170             :           "      imported_rsa: %d\n"
     171             :           "         unchanged: %d\n"
     172             :           "      new user ids: %d\n"
     173             :           "       new subkeys: %d\n"
     174             :           "    new signatures: %d\n"
     175             :           "   new revocations: %d\n"
     176             :           "       secret read: %d\n"
     177             :           "   secret imported: %d\n"
     178             :           "  secret unchanged: %d\n"
     179             :           "  skipped new keys: %d\n"
     180             :           "      not imported: %d\n"
     181             :           "   skipped v3 keys: %d\n",
     182             :           r->considered,
     183             :           r->no_user_id,
     184             :           r->imported,
     185             :           r->imported_rsa,
     186             :           r->unchanged,
     187             :           r->new_user_ids,
     188             :           r->new_sub_keys,
     189             :           r->new_signatures,
     190             :           r->new_revocations,
     191             :           r->secret_read,
     192             :           r->secret_imported,
     193             :           r->secret_unchanged,
     194             :           r->skipped_new_keys,
     195             :           r->not_imported,
     196             :           r->skipped_v3_keys);
     197           0 : }
     198             : 

Generated by: LCOV version 1.13