6dc14569b041a7484cc63a91ad5ec8cc02ddeeb3
[gpgme.git] / tests / gpgsm / t-support.h
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 #include <gpgme.h>
28
29 #define fail_if_err(err)                                        \
30   do                                                            \
31     {                                                           \
32       if (err)                                                  \
33         {                                                       \
34           fprintf (stderr, "%s:%d: %s: %s (%d.%d)\n",           \
35                    __FILE__, __LINE__, gpg_strsource (err),     \
36                    gpg_strerror (err),                          \
37                    gpg_err_source (err), gpg_err_code (err));   \
38           exit (1);                                             \
39         }                                                       \
40     }                                                           \
41   while (0)
42
43
44 void
45 print_data (gpgme_data_t dh)
46 {
47 #define BUF_SIZE 512
48   char buf[BUF_SIZE + 1];
49   int ret;
50   
51   ret = gpgme_data_seek (dh, 0, SEEK_SET);
52   if (ret)
53     fail_if_err (gpg_error_from_errno (errno));
54   while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
55     fwrite (buf, ret, 1, stdout);
56   if (ret < 0)
57     fail_if_err (gpg_error_from_errno (errno));
58 }
59
60
61 gpgme_error_t
62 passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info,
63                int last_was_bad, int fd)
64 {
65   int res;
66   char *pass = "abc\n";
67   int passlen = strlen (pass);
68   int off = 0;
69
70   do
71     {
72       res = write (fd, &pass[off], passlen - off);
73       if (res > 0)
74         off += res;
75     }
76   while (res > 0 && off != passlen);
77
78   return off == passlen ? 0 : gpgme_error_from_errno (errno);
79 }
80
81
82 char *
83 make_filename (const char *fname)
84 {
85   const char *srcdir = getenv ("srcdir");
86   char *buf;
87
88   if (!srcdir)
89     srcdir = ".";
90   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
91   if (!buf) 
92     exit (8);
93   strcpy (buf, srcdir);
94   strcat (buf, "/");
95   strcat (buf, fname);
96   return buf;
97 }
98
99
100 void
101 init_gpgme (gpgme_protocol_t proto)
102 {
103   gpgme_error_t err;
104
105   gpgme_check_version (NULL);
106 #ifndef HAVE_W32_SYSTEM
107   setlocale (LC_ALL, "");
108   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
109   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
110 #endif
111
112   err = gpgme_engine_check_version (proto);
113   fail_if_err (err);
114 }