LCOV - code coverage report
Current view: top level - tests/gpg - t-support.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 55 43.6 %
Date: 2016-12-01 18:45:36 Functions: 3 6 50.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             : #ifdef HAVE_W32_SYSTEM
      28             : #include <windows.h>
      29             : #endif
      30             : 
      31             : #include <gpgme.h>
      32             : 
      33             : #ifndef DIM
      34             : #define DIM(v)               (sizeof(v)/sizeof((v)[0]))
      35             : #endif
      36             : 
      37             : #define fail_if_err(err)                                        \
      38             :   do                                                            \
      39             :     {                                                           \
      40             :       if (err)                                                  \
      41             :         {                                                       \
      42             :           fprintf (stderr, "%s:%d: %s: %s\n",                 \
      43             :                    __FILE__, __LINE__, gpgme_strsource (err),   \
      44             :                    gpgme_strerror (err));                       \
      45             :           exit (1);                                             \
      46             :         }                                                       \
      47             :     }                                                           \
      48             :   while (0)
      49             : 
      50             : 
      51             : static const char *
      52           0 : nonnull (const char *s)
      53             : {
      54           0 :   return s? s :"[none]";
      55             : }
      56             : 
      57             : 
      58             : void
      59          18 : print_data (gpgme_data_t dh)
      60             : {
      61             : #define BUF_SIZE 512
      62             :   char buf[BUF_SIZE + 1];
      63             :   int ret;
      64             : 
      65          18 :   ret = gpgme_data_seek (dh, 0, SEEK_SET);
      66          18 :   if (ret)
      67           0 :     fail_if_err (gpgme_err_code_from_errno (errno));
      68          71 :   while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
      69          35 :     fwrite (buf, ret, 1, stdout);
      70          18 :   if (ret < 0)
      71           0 :     fail_if_err (gpgme_err_code_from_errno (errno));
      72          18 : }
      73             : 
      74             : 
      75             : gpgme_error_t
      76           0 : passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info,
      77             :                int last_was_bad, int fd)
      78             : {
      79             :   int res;
      80           0 :   char pass[] = "abc\n";
      81           0 :   int passlen = strlen (pass);
      82           0 :   int off = 0;
      83             : 
      84             :   (void)opaque;
      85             :   (void)uid_hint;
      86             :   (void)passphrase_info;
      87             :   (void)last_was_bad;
      88             : 
      89             :   do
      90             :     {
      91           0 :       res = gpgme_io_write (fd, &pass[off], passlen - off);
      92           0 :       if (res > 0)
      93           0 :         off += res;
      94             :     }
      95           0 :   while (res > 0 && off != passlen);
      96             : 
      97           0 :   return off == passlen ? 0 : gpgme_error_from_errno (errno);
      98             : }
      99             : 
     100             : 
     101             : char *
     102           5 : make_filename (const char *fname)
     103             : {
     104           5 :   const char *srcdir = getenv ("srcdir");
     105             :   char *buf;
     106             : 
     107           5 :   if (!srcdir)
     108           0 :     srcdir = ".";
     109           5 :   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
     110           5 :   if (!buf)
     111           0 :     exit (8);
     112           5 :   strcpy (buf, srcdir);
     113           5 :   strcat (buf, "/");
     114           5 :   strcat (buf, fname);
     115           5 :   return buf;
     116             : }
     117             : 
     118             : 
     119             : void
     120          43 : init_gpgme (gpgme_protocol_t proto)
     121             : {
     122             :   gpgme_error_t err;
     123             : 
     124          43 :   gpgme_check_version (NULL);
     125          43 :   setlocale (LC_ALL, "");
     126          43 :   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
     127             : #ifndef HAVE_W32_SYSTEM
     128          43 :   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
     129             : #endif
     130             : 
     131          43 :   err = gpgme_engine_check_version (proto);
     132          43 :   fail_if_err (err);
     133          43 : }
     134             : 
     135             : 
     136             : void
     137           0 : print_import_result (gpgme_import_result_t r)
     138             : {
     139             :   gpgme_import_status_t st;
     140             : 
     141           0 :   for (st=r->imports; st; st = st->next)
     142             :     {
     143           0 :       printf ("  fpr: %s err: %d (%s) status:", nonnull (st->fpr),
     144             :               st->result, gpgme_strerror (st->result));
     145           0 :       if (st->status & GPGME_IMPORT_NEW)
     146           0 :         fputs (" new", stdout);
     147           0 :       if (st->status & GPGME_IMPORT_UID)
     148           0 :         fputs (" uid", stdout);
     149           0 :       if (st->status & GPGME_IMPORT_SIG)
     150           0 :         fputs (" sig", stdout);
     151           0 :       if (st->status & GPGME_IMPORT_SUBKEY)
     152           0 :         fputs (" subkey", stdout);
     153           0 :       if (st->status & GPGME_IMPORT_SECRET)
     154           0 :         fputs (" secret", stdout);
     155           0 :       putchar ('\n');
     156             :     }
     157           0 :   printf ("key import summary:\n"
     158             :           "        considered: %d\n"
     159             :           "        no user id: %d\n"
     160             :           "          imported: %d\n"
     161             :           "      imported_rsa: %d\n"
     162             :           "         unchanged: %d\n"
     163             :           "      new user ids: %d\n"
     164             :           "       new subkeys: %d\n"
     165             :           "    new signatures: %d\n"
     166             :           "   new revocations: %d\n"
     167             :           "       secret read: %d\n"
     168             :           "   secret imported: %d\n"
     169             :           "  secret unchanged: %d\n"
     170             :           "  skipped new keys: %d\n"
     171             :           "      not imported: %d\n",
     172             :           r->considered,
     173             :           r->no_user_id,
     174             :           r->imported,
     175             :           r->imported_rsa,
     176             :           r->unchanged,
     177             :           r->new_user_ids,
     178             :           r->new_sub_keys,
     179             :           r->new_signatures,
     180             :           r->new_revocations,
     181             :           r->secret_read,
     182             :           r->secret_imported,
     183             :           r->secret_unchanged,
     184             :           r->skipped_new_keys,
     185             :           r->not_imported);
     186           0 : }
     187             : 

Generated by: LCOV version 1.11