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: 2015-11-05 17:14:26 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          16 : print_data (gpgme_data_t dh)
      60             : {
      61             : #define BUF_SIZE 512
      62             :   char buf[BUF_SIZE + 1];
      63             :   int ret;
      64             :   
      65          16 :   ret = gpgme_data_seek (dh, 0, SEEK_SET);
      66          16 :   if (ret)
      67           0 :     fail_if_err (gpgme_err_code_from_errno (errno));
      68          64 :   while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
      69          32 :     fwrite (buf, ret, 1, stdout);
      70          16 :   if (ret < 0)
      71           0 :     fail_if_err (gpgme_err_code_from_errno (errno));
      72          16 : }
      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             : #ifdef HAVE_W32_SYSTEM
      80             :   DWORD written;
      81             :   WriteFile ((HANDLE) fd, "abc\n", 4, &written, 0);
      82             : #else
      83             :   int res;
      84           0 :   char *pass = "abc\n";
      85           0 :   int passlen = strlen (pass);
      86           0 :   int off = 0;
      87             : 
      88             :   do
      89             :     {
      90           0 :       res = 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             : #endif
      98             : 
      99             :   return 0;
     100             : }
     101             : 
     102             : 
     103             : char *
     104           5 : make_filename (const char *fname)
     105             : {
     106           5 :   const char *srcdir = getenv ("srcdir");
     107             :   char *buf;
     108             : 
     109           5 :   if (!srcdir)
     110           0 :     srcdir = ".";
     111           5 :   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
     112           5 :   if (!buf) 
     113           0 :     exit (8);
     114           5 :   strcpy (buf, srcdir);
     115           5 :   strcat (buf, "/");
     116           5 :   strcat (buf, fname);
     117           5 :   return buf;
     118             : }
     119             : 
     120             : 
     121             : void
     122          40 : init_gpgme (gpgme_protocol_t proto)
     123             : {
     124             :   gpgme_error_t err;
     125             : 
     126          40 :   gpgme_check_version (NULL);
     127          40 :   setlocale (LC_ALL, "");
     128          40 :   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
     129             : #ifndef HAVE_W32_SYSTEM
     130          40 :   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
     131             : #endif
     132             : 
     133          40 :   err = gpgme_engine_check_version (proto);
     134          40 :   fail_if_err (err);
     135          40 : }
     136             : 
     137             : 
     138             : void
     139           0 : print_import_result (gpgme_import_result_t r)
     140             : {
     141             :   gpgme_import_status_t st;
     142             : 
     143           0 :   for (st=r->imports; st; st = st->next)
     144             :     {
     145           0 :       printf ("  fpr: %s err: %d (%s) status:", nonnull (st->fpr),
     146             :               st->result, gpgme_strerror (st->result));
     147           0 :       if (st->status & GPGME_IMPORT_NEW)
     148           0 :         fputs (" new", stdout);
     149           0 :       if (st->status & GPGME_IMPORT_UID)
     150           0 :         fputs (" uid", stdout);
     151           0 :       if (st->status & GPGME_IMPORT_SIG)
     152           0 :         fputs (" sig", stdout);
     153           0 :       if (st->status & GPGME_IMPORT_SUBKEY)
     154           0 :         fputs (" subkey", stdout);
     155           0 :       if (st->status & GPGME_IMPORT_SECRET)
     156           0 :         fputs (" secret", stdout);
     157           0 :       putchar ('\n');
     158             :     }
     159           0 :   printf ("key import summary:\n"
     160             :           "        considered: %d\n"
     161             :           "        no user id: %d\n"
     162             :           "          imported: %d\n"
     163             :           "      imported_rsa: %d\n"
     164             :           "         unchanged: %d\n"
     165             :           "      new user ids: %d\n"
     166             :           "       new subkeys: %d\n"
     167             :           "    new signatures: %d\n"
     168             :           "   new revocations: %d\n"
     169             :           "       secret read: %d\n"
     170             :           "   secret imported: %d\n"
     171             :           "  secret unchanged: %d\n"
     172             :           "  skipped new keys: %d\n"
     173             :           "      not imported: %d\n",
     174             :           r->considered,
     175             :           r->no_user_id,
     176             :           r->imported,
     177             :           r->imported_rsa,
     178             :           r->unchanged,
     179             :           r->new_user_ids,
     180             :           r->new_sub_keys,
     181             :           r->new_signatures,
     182             :           r->new_revocations,
     183             :           r->secret_read,
     184             :           r->secret_imported,
     185             :           r->secret_unchanged,
     186             :           r->skipped_new_keys,
     187             :           r->not_imported);
     188           0 : }
     189             : 

Generated by: LCOV version 1.11