Update GPGME test suite for use with GnuPG 2.1.
[gpgme.git] / tests / gpg / t-import.c
1 /* t-import.c - Regression test.
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 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 /* We need to include config.h so that we know whether we are building
23    with large file system (LFS) support. */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include <gpgme.h>
34
35 #include "t-support.h"
36
37 \f
38 void
39 check_result (gpgme_import_result_t result, char *fpr, int secret)
40 {
41   if (result->considered != 1 && (secret && result->considered != 3))
42     {
43       fprintf (stderr, "Unexpected number of considered keys %i\n",
44                result->considered);
45       exit (1);
46     }
47   if (result->no_user_id != 0)
48     {
49       fprintf (stderr, "Unexpected number of user ids %i\n",
50                result->no_user_id);
51       exit (1);
52     }
53   if ((secret && result->imported != 0)
54       || (!secret && (result->imported != 0 && result->imported != 1)))
55     {
56       fprintf (stderr, "Unexpected number of imported keys %i\n",
57                result->imported);
58       exit (1);
59     }
60   if (result->imported_rsa != 0)
61     {
62       fprintf (stderr, "Unexpected number of imported RSA keys %i\n",
63                result->imported_rsa);
64       exit (1);
65     }
66   if ((secret && (result->unchanged != 0 && result->unchanged != 1))
67       || (!secret && ((result->imported == 0 && result->unchanged != 1)
68                       || (result->imported == 1 && result->unchanged != 0))))
69     {
70       fprintf (stderr, "Unexpected number of unchanged keys %i\n",
71                result->unchanged);
72       exit (1);
73     }
74   if (result->new_user_ids != 0)
75     {
76       fprintf (stderr, "Unexpected number of new user IDs %i\n",
77                result->new_user_ids);
78       exit (1);
79     }
80   if (result->new_sub_keys != 0)
81     {
82       fprintf (stderr, "Unexpected number of new sub keys %i\n",
83                result->new_sub_keys);
84       exit (1);
85     }
86   if ((secret
87        && ((result->secret_imported == 0 && result->new_signatures != 0)
88            || (result->secret_imported == 1 && result->new_signatures > 1)))
89       || (!secret && result->new_signatures != 0))
90     {
91       fprintf (stderr, "Unexpected number of new signatures %i\n",
92                result->new_signatures);
93       if (result->new_signatures == 2)
94         fprintf (stderr, "### ignored due to gpg 1.3.4 problems\n");
95       else
96         exit (1);
97     }
98   if (result->new_revocations != 0)
99     {
100       fprintf (stderr, "Unexpected number of new revocations %i\n",
101                result->new_revocations);
102       exit (1);
103     }
104   if ((secret && result->secret_read != 1 && result->secret_read != 3)
105       || (!secret && result->secret_read != 0))
106     {
107       fprintf (stderr, "Unexpected number of secret keys read %i\n",
108                result->secret_read);
109       exit (1);
110     }
111   if ((secret && result->secret_imported != 0 && result->secret_imported != 1)
112       || (!secret && result->secret_imported != 0))
113     {
114       fprintf (stderr, "Unexpected number of secret keys imported %i\n",
115                result->secret_imported);
116       exit (1);
117     }
118   if ((secret
119        && ((result->secret_imported == 0 && result->secret_unchanged != 1
120             && result->secret_unchanged != 2)
121            || (result->secret_imported == 1 && result->secret_unchanged != 0)))
122       || (!secret && result->secret_unchanged != 0))
123     {
124       fprintf (stderr, "Unexpected number of secret keys unchanged %i\n",
125                result->secret_unchanged);
126       exit (1);
127     }
128   if (result->not_imported != 0)
129     {
130       fprintf (stderr, "Unexpected number of secret keys not imported %i\n",
131                result->not_imported);
132       exit (1);
133     }
134   if (secret)
135     {
136       if (!result->imports
137           || (result->imports->next && result->imports->next->next))
138         {
139           fprintf (stderr, "Unexpected number of status reports\n");
140           exit (1);
141         }
142     }
143   else
144     {
145       if (!result->imports || result->imports->next)
146         {
147           fprintf (stderr, "Unexpected number of status reports\n");
148           exit (1);
149         }
150     }
151   if (strcmp (fpr, result->imports->fpr))
152     {
153       fprintf (stderr, "Unexpected fingerprint %s\n",
154                result->imports->fpr);
155       exit (1);
156     }
157   if (result->imports->next && strcmp (fpr, result->imports->next->fpr))
158     {
159       fprintf (stderr, "Unexpected fingerprint on second status %s\n",
160                result->imports->next->fpr);
161       exit (1);
162     }
163   if (result->imports->result != 0)
164     {
165       fprintf (stderr, "Unexpected status result %s\n",
166                gpgme_strerror (result->imports->result));
167       exit (1);
168     }
169   if (secret)
170     {
171       if (result->secret_imported == 0)
172         {
173           if (result->imports->status != GPGME_IMPORT_SECRET)
174             {
175               fprintf (stderr, "Unexpected status %i\n",
176                        result->imports->status);
177               exit (1);
178             }
179         }
180       else
181         {
182           if (result->imports->status
183               != (GPGME_IMPORT_SECRET | GPGME_IMPORT_NEW)
184               || (result->imports->next
185                   && result->imports->next->status != GPGME_IMPORT_SIG))
186             {
187               fprintf (stderr, "Unexpected status %i\n",
188                        result->imports->status);
189               exit (1);
190             }
191         }
192     }
193   else
194     {
195       if ((result->imported == 0 && result->imports->status != 0)
196           || (result->imported == 1
197               && result->imports->status != GPGME_IMPORT_NEW))
198         {
199           fprintf (stderr, "Unexpected status %i\n",
200                    result->imports->status);
201           exit (1);
202         }
203     }
204 }
205
206
207 int 
208 main (int argc, char *argv[])
209 {
210   gpgme_ctx_t ctx;
211   gpgme_error_t err;
212   gpgme_data_t in;
213   gpgme_import_result_t result;
214   const char *pubkey_1_asc = make_filename ("pubkey-1.asc");
215   const char *seckey_1_asc = make_filename ("seckey-1.asc");
216
217   init_gpgme (GPGME_PROTOCOL_OpenPGP);
218
219   err = gpgme_new (&ctx);
220   fail_if_err (err);
221
222   err = gpgme_data_new_from_file (&in, pubkey_1_asc, 1);
223   fail_if_err (err);
224
225   err = gpgme_op_import (ctx, in);
226   fail_if_err (err);
227   result = gpgme_op_import_result (ctx);
228   check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 0);
229   gpgme_data_release (in);
230
231   err = gpgme_data_new_from_file (&in, seckey_1_asc, 1);
232   fail_if_err (err);
233
234   err = gpgme_op_import (ctx, in);
235   fail_if_err (err);
236   result = gpgme_op_import_result (ctx);
237   check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 1);
238   gpgme_data_release (in);
239
240   gpgme_release (ctx);
241   return 0;
242 }