Blame tests/gpgsm/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
#include <gpgme.h>
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 (%d.%d)\n",        	\
Packit Service 672cf4
                   __FILE__, __LINE__, gpgme_strsource (err),	\
Packit Service 672cf4
		   gpgme_strerror (err),                        \
Packit Service 672cf4
                   gpgme_err_source (err), gpgme_err_code (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
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_error_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_error_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
#ifndef HAVE_W32_SYSTEM
Packit Service 672cf4
  setlocale (LC_ALL, "");
Packit Service 672cf4
  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
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
}