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, see <https://gnu.org/licenses/>.
19  * SPDX-License-Identifier: LGPL-2.1-or-later
20  */
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 
38 void
check_result(gpgme_import_result_t result,const char * fpr,int secret)39 check_result (gpgme_import_result_t result, const 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        && result->secret_imported != 2)
113       || (!secret && result->secret_imported != 0))
114     {
115       fprintf (stderr, "Unexpected number of secret keys imported %i\n",
116 	       result->secret_imported);
117       exit (1);
118     }
119   if ((secret
120        && ((result->secret_imported == 0 && result->secret_unchanged != 1
121 	    && result->secret_unchanged != 2)
122 	   || (result->secret_imported == 1 && result->secret_unchanged != 0)))
123       || (!secret && result->secret_unchanged != 0))
124     {
125       fprintf (stderr, "Unexpected number of secret keys unchanged %i\n",
126 	       result->secret_unchanged);
127       exit (1);
128     }
129   if (result->not_imported != 0)
130     {
131       fprintf (stderr, "Unexpected number of secret keys not imported %i\n",
132 	       result->not_imported);
133       exit (1);
134     }
135   if (secret)
136     {
137       if (!result->imports
138 	  || (result->imports->next && result->imports->next->next))
139 	{
140 	  fprintf (stderr, "Unexpected number of status reports\n");
141 	  exit (1);
142 	}
143     }
144   else
145     {
146       if (!result->imports || result->imports->next)
147 	{
148 	  fprintf (stderr, "Unexpected number of status reports\n");
149 	  exit (1);
150 	}
151     }
152   if (strcmp (fpr, result->imports->fpr))
153     {
154       fprintf (stderr, "Unexpected fingerprint %s\n",
155 	       result->imports->fpr);
156       exit (1);
157     }
158   if (result->imports->next && strcmp (fpr, result->imports->next->fpr))
159     {
160       fprintf (stderr, "Unexpected fingerprint on second status %s\n",
161 	       result->imports->next->fpr);
162       exit (1);
163     }
164   if (result->imports->result != 0)
165     {
166       fprintf (stderr, "Unexpected status result %s\n",
167 	       gpgme_strerror (result->imports->result));
168       exit (1);
169     }
170 #if 0
171   if (secret)
172     {
173       if (result->secret_imported == 0)
174 	{
175 	  if (result->imports->status != GPGME_IMPORT_SECRET)
176 	    {
177 	      fprintf (stderr, "Unexpected status %i\n",
178 		       result->imports->status);
179 	      exit (1);
180 	    }
181 	}
182       else
183 	{
184 	  if (result->imports->status
185 	      != (GPGME_IMPORT_SECRET | GPGME_IMPORT_NEW)
186 	      || (result->imports->next
187 		  && result->imports->next->status != GPGME_IMPORT_SIG))
188 	    {
189 	      fprintf (stderr, "Unexpected status %i\n",
190 		       result->imports->status);
191 	      exit (1);
192 	    }
193 	}
194     }
195   else
196     {
197       if ((result->imported == 0 && result->imports->status != 0)
198 	  || (result->imported == 1
199 	      && result->imports->status != GPGME_IMPORT_NEW))
200 	{
201 	  fprintf (stderr, "Unexpected status %i\n",
202 		   result->imports->status);
203 	  exit (1);
204 	}
205     }
206 #endif
207 }
208 
209 
210 int
main(int argc,char * argv[])211 main (int argc, char *argv[])
212 {
213   gpgme_ctx_t ctx;
214   gpgme_error_t err;
215   gpgme_data_t in;
216   gpgme_import_result_t result;
217   char *pubkey_1_asc = make_filename ("pubkey-1.asc");
218   char *seckey_1_asc = make_filename ("seckey-1.asc");
219 
220   (void)argc;
221   (void)argv;
222 
223   init_gpgme (GPGME_PROTOCOL_OpenPGP);
224 
225   err = gpgme_new (&ctx);
226   fail_if_err (err);
227 
228   err = gpgme_data_new_from_file (&in, pubkey_1_asc, 1);
229   free (pubkey_1_asc);
230   fail_if_err (err);
231 
232   err = gpgme_op_import (ctx, in);
233   fail_if_err (err);
234   result = gpgme_op_import_result (ctx);
235   check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 0);
236   gpgme_data_release (in);
237 
238   err = gpgme_data_new_from_file (&in, seckey_1_asc, 1);
239   free (seckey_1_asc);
240   fail_if_err (err);
241 
242   err = gpgme_op_import (ctx, in);
243   fail_if_err (err);
244   result = gpgme_op_import_result (ctx);
245   check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 1);
246   gpgme_data_release (in);
247 
248   gpgme_release (ctx);
249   return 0;
250 }
251