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