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