Blame tests/gpg/t-support.h

Packit Service 672cf4
/* t-support.h - Helper routines for regression tests.
Packit Service 6c01f9
   Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, write to the Free Software
Packit Service 6c01f9
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 6c01f9
   02111-1307, USA.  */
Packit Service 672cf4
Packit Service 672cf4
#include <unistd.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <locale.h>
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_W32_SYSTEM
Packit Service 672cf4
#include <windows.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
Packit Service 672cf4
#ifndef DIM
Packit Service 672cf4
#define DIM(v)		     (sizeof(v)/sizeof((v)[0]))
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#define fail_if_err(err)					\
Packit Service 672cf4
  do								\
Packit Service 672cf4
    {								\
Packit Service 672cf4
      if (err)							\
Packit Service 672cf4
        {							\
Packit Service 672cf4
          fprintf (stderr, "%s:%d: %s: %s\n",			\
Packit Service 672cf4
                   __FILE__, __LINE__, gpgme_strsource (err),	\
Packit Service 672cf4
		   gpgme_strerror (err));			\
Packit Service 672cf4
          exit (1);						\
Packit Service 672cf4
        }							\
Packit Service 672cf4
    }								\
Packit Service 672cf4
  while (0)
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
#ifdef GPGRT_HAVE_MACRO_FUNCTION
Packit Service 672cf4
void GPGRT_ATTR_NORETURN
Packit Service 672cf4
_test (const char *expr, const char *file, int line,
Packit Service 672cf4
       const char *func)
Packit Service 672cf4
{
Packit Service 672cf4
  fprintf (stderr, "Test \"%s\" in %s failed (%s:%d)\n",
Packit Service 672cf4
           expr, func, file, line);
Packit Service 672cf4
  exit (1);
Packit Service 672cf4
}
Packit Service 672cf4
# define test(expr)                                             \
Packit Service 672cf4
  ((expr)                                                       \
Packit Service 672cf4
   ? (void) 0                                                   \
Packit Service 672cf4
   : _test (#expr, __FILE__, __LINE__, __FUNCTION__))
Packit Service 672cf4
#else /*!GPGRT_HAVE_MACRO_FUNCTION*/
Packit Service 672cf4
void
Packit Service 672cf4
_test (const char *expr, const char *file, int line)
Packit Service 672cf4
{
Packit Service 672cf4
  fprintf (stderr, "Test \"%s\" failed (%s:%d)\n",
Packit Service 672cf4
           expr, file, line);
Packit Service 672cf4
  exit (1);
Packit Service 672cf4
}
Packit Service 672cf4
# define test(expr)                                             \
Packit Service 672cf4
  ((expr)                                                       \
Packit Service 672cf4
   ? (void) 0                                                   \
Packit Service 672cf4
   : _test (#expr, __FILE__, __LINE__))
Packit Service 672cf4
#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static const char *
Packit Service 672cf4
nonnull (const char *s)
Packit Service 672cf4
{
Packit Service 672cf4
  return s? s :"[none]";
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
print_data (gpgme_data_t dh)
Packit Service 672cf4
{
Packit Service 672cf4
#define BUF_SIZE 512
Packit Service 672cf4
  char buf[BUF_SIZE + 1];
Packit Service 672cf4
  int ret;
Packit Service 672cf4
Packit Service 672cf4
  ret = gpgme_data_seek (dh, 0, SEEK_SET);
Packit Service 672cf4
  if (ret)
Packit Service 672cf4
    fail_if_err (gpgme_err_code_from_errno (errno));
Packit Service 672cf4
  while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
Packit Service 672cf4
    fwrite (buf, ret, 1, stdout);
Packit Service 672cf4
  if (ret < 0)
Packit Service 672cf4
    fail_if_err (gpgme_err_code_from_errno (errno));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info,
Packit Service 672cf4
	       int last_was_bad, int fd)
Packit Service 672cf4
{
Packit Service 672cf4
  int res;
Packit Service 672cf4
  char pass[] = "abc\n";
Packit Service 672cf4
  int passlen = strlen (pass);
Packit Service 672cf4
  int off = 0;
Packit Service 672cf4
Packit Service 672cf4
  (void)opaque;
Packit Service 672cf4
  (void)uid_hint;
Packit Service 672cf4
  (void)passphrase_info;
Packit Service 672cf4
  (void)last_was_bad;
Packit Service 672cf4
Packit Service 672cf4
  do
Packit Service 672cf4
    {
Packit Service 672cf4
      res = gpgme_io_write (fd, &pass[off], passlen - off);
Packit Service 672cf4
      if (res > 0)
Packit Service 672cf4
	off += res;
Packit Service 672cf4
    }
Packit Service 672cf4
  while (res > 0 && off != passlen);
Packit Service 672cf4
Packit Service 672cf4
  return off == passlen ? 0 : gpgme_error_from_errno (errno);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
char *
Packit Service 672cf4
make_filename (const char *fname)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *srcdir = getenv ("srcdir");
Packit Service 672cf4
  char *buf;
Packit Service 672cf4
Packit Service 672cf4
  if (!srcdir)
Packit Service 672cf4
    srcdir = ".";
Packit Service 672cf4
  buf = malloc (strlen(srcdir) + strlen(fname) + 2);
Packit Service 672cf4
  if (!buf)
Packit Service 672cf4
    exit (8);
Packit Service 672cf4
  strcpy (buf, srcdir);
Packit Service 672cf4
  strcat (buf, "/");
Packit Service 672cf4
  strcat (buf, fname);
Packit Service 672cf4
  return buf;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
init_gpgme (gpgme_protocol_t proto)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 672cf4
  gpgme_check_version (NULL);
Packit Service 672cf4
  setlocale (LC_ALL, "");
Packit Service 672cf4
  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
Packit Service 672cf4
#ifndef HAVE_W32_SYSTEM
Packit Service 672cf4
  gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_engine_check_version (proto);
Packit Service 672cf4
  fail_if_err (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
print_import_result (gpgme_import_result_t r)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_import_status_t st;
Packit Service 672cf4
Packit Service 672cf4
  for (st=r->imports; st; st = st->next)
Packit Service 672cf4
    {
Packit Service 672cf4
      printf ("  fpr: %s err: %d (%s) status:", nonnull (st->fpr),
Packit Service 672cf4
              st->result, gpgme_strerror (st->result));
Packit Service 672cf4
      if (st->status & GPGME_IMPORT_NEW)
Packit Service 672cf4
        fputs (" new", stdout);
Packit Service 672cf4
      if (st->status & GPGME_IMPORT_UID)
Packit Service 672cf4
        fputs (" uid", stdout);
Packit Service 672cf4
      if (st->status & GPGME_IMPORT_SIG)
Packit Service 672cf4
        fputs (" sig", stdout);
Packit Service 672cf4
      if (st->status & GPGME_IMPORT_SUBKEY)
Packit Service 672cf4
        fputs (" subkey", stdout);
Packit Service 672cf4
      if (st->status & GPGME_IMPORT_SECRET)
Packit Service 672cf4
        fputs (" secret", stdout);
Packit Service 672cf4
      putchar ('\n');
Packit Service 672cf4
    }
Packit Service 672cf4
  printf ("key import summary:\n"
Packit Service 672cf4
          "        considered: %d\n"
Packit Service 672cf4
          "        no user id: %d\n"
Packit Service 672cf4
          "          imported: %d\n"
Packit Service 672cf4
          "      imported_rsa: %d\n"
Packit Service 672cf4
          "         unchanged: %d\n"
Packit Service 672cf4
          "      new user ids: %d\n"
Packit Service 672cf4
          "       new subkeys: %d\n"
Packit Service 672cf4
          "    new signatures: %d\n"
Packit Service 672cf4
          "   new revocations: %d\n"
Packit Service 672cf4
          "       secret read: %d\n"
Packit Service 672cf4
          "   secret imported: %d\n"
Packit Service 672cf4
          "  secret unchanged: %d\n"
Packit Service 672cf4
          "  skipped new keys: %d\n"
Packit Service 6c01f9
          "      not imported: %d\n",
Packit Service 672cf4
          r->considered,
Packit Service 672cf4
          r->no_user_id,
Packit Service 672cf4
          r->imported,
Packit Service 672cf4
          r->imported_rsa,
Packit Service 672cf4
          r->unchanged,
Packit Service 672cf4
          r->new_user_ids,
Packit Service 672cf4
          r->new_sub_keys,
Packit Service 672cf4
          r->new_signatures,
Packit Service 672cf4
          r->new_revocations,
Packit Service 672cf4
          r->secret_read,
Packit Service 672cf4
          r->secret_imported,
Packit Service 672cf4
          r->secret_unchanged,
Packit Service 672cf4
          r->skipped_new_keys,
Packit Service 6c01f9
          r->not_imported);
Packit Service 672cf4
}
Packit Service 6c01f9