2003-07-31 Marcus Brinkmann <marcus@g10code.de>
[gpgme.git] / tests / gpg / t-keylist-sig.c
1 /* t-keylist-sig.c - Regression test.
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2003 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 General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (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    General Public License for more details.
16  
17    You should have received a copy of the GNU General Public License
18    along with GPGME; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <gpgme.h>
26
27 #include "t-support.h"
28
29 \f
30 struct
31 {
32   char *fpr;
33   char *sec_keyid;
34   struct
35   {
36     char *name;
37     char *comment;
38     char *email;
39     struct
40     {
41       gpgme_pubkey_algo_t algo;
42       char *keyid;
43       char *name;
44       char *comment;
45       char *email;
46       unsigned int class;
47       int exportable;
48     } sig;
49   } uid[3];  
50 }
51 keys[] =
52   {
53     { "A0FF4590BB6122EDEF6E3C542D727CC768697734", "6AE6D7EE46A871F8",
54       { { "Alfa Test", "demo key", "alfa@example.net",
55           { GPGME_PK_DSA, "2D727CC768697734",
56             "Alfa Test", "demo key", "alfa@example.net", 19, 1 } },
57         { "Alpha Test", "demo key", "alpha@example.net",
58           { GPGME_PK_DSA, "2D727CC768697734",
59             "Alfa Test", "demo key", "alfa@example.net", 19, 1 } },
60         { "Alice", "demo key", NULL,
61           { GPGME_PK_DSA, "2D727CC768697734",
62             "Alfa Test", "demo key", "alfa@example.net", 19, 1 } } } },
63     { NULL }
64   };
65
66
67 int 
68 main (int argc, char **argv)
69 {
70   gpgme_error_t err;
71   gpgme_ctx_t ctx;
72   gpgme_key_t key;
73   gpgme_keylist_result_t result;
74   int mode;
75   int i = 0;
76
77   err = gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP);
78   fail_if_err (err);
79
80   err = gpgme_new (&ctx);
81   fail_if_err (err);
82
83   mode  = gpgme_get_keylist_mode (ctx);
84   mode |= GPGME_KEYLIST_MODE_SIGS;
85   err = gpgme_set_keylist_mode (ctx, mode);
86   fail_if_err (err);
87
88   err = gpgme_op_keylist_start (ctx, "Alpha", 0);
89   fail_if_err (err);
90     
91   while (!(err = gpgme_op_keylist_next (ctx, &key)))
92     {
93       if (!keys[i].fpr)
94         {
95           fprintf (stderr, "More keys returned than expected\n");
96           exit (1);
97         }
98
99       /* Global key flags.  */
100       if (key->revoked)
101         {
102           fprintf (stderr, "Key unexpectedly revoked\n");
103           exit (1);
104         }
105       if (key->expired)
106         {
107           fprintf (stderr, "Key unexpectedly expired\n");
108           exit (1);
109         }
110       if (key->disabled)
111         {
112           fprintf (stderr, "Key unexpectedly disabled\n");
113           exit (1);
114         }
115       if (key->invalid)
116         {
117           fprintf (stderr, "Key unexpectedly invalid\n");
118           exit (1);
119         }
120       if (!key->can_encrypt)
121         {
122           fprintf (stderr, "Key unexpectedly unusable for encryption\n");
123           exit (1);
124         }
125       if (!key->can_sign)
126         {
127           fprintf (stderr, "Key unexpectedly unusable for signing\n");
128           exit (1);
129         }
130       if (!key->can_certify)
131         {
132           fprintf (stderr, "Key unexpectedly unusable for certifications\n");
133           exit (1);
134         }
135       if (key->secret)
136         {
137           fprintf (stderr, "Key unexpectedly secret\n");
138           exit (1);
139         }
140       if (key->protocol != GPGME_PROTOCOL_OpenPGP)
141         {
142           fprintf (stderr, "Key has unexpected protocol: %s\n",
143                    gpgme_get_protocol_name (key->protocol));
144           exit (1);
145         }
146       if (key->issuer_serial)
147         {
148           fprintf (stderr, "Key unexpectedly carries issuer serial: %s\n",
149                    key->issuer_serial);
150           exit (1);
151         }
152       if (key->issuer_name)
153         {
154           fprintf (stderr, "Key unexpectedly carries issuer name: %s\n",
155                    key->issuer_name);
156           exit (1);
157         }
158       if (key->chain_id)
159         {
160           fprintf (stderr, "Key unexpectedly carries chain ID: %s\n",
161                    key->chain_id);
162           exit (1);
163         }
164       if (key->owner_trust != GPGME_VALIDITY_UNKNOWN)
165         {
166           fprintf (stderr, "Key has unexpected owner trust: %i\n",
167                    key->owner_trust);
168           exit (1);
169         }
170       if (!key->subkeys || !key->subkeys->next || key->subkeys->next->next)
171         {
172           fprintf (stderr, "Key has unexpected number of subkeys\n");
173           exit (1);
174         }
175
176       /* Primary key.  */
177       if (key->subkeys->revoked)
178         {
179           fprintf (stderr, "Primary key unexpectedly revoked\n");
180           exit (1);
181         }
182       if (key->subkeys->expired)
183         {
184           fprintf (stderr, "Primary key unexpectedly expired\n");
185           exit (1);
186         }
187       if (key->subkeys->disabled)
188         {
189           fprintf (stderr, "Primary key unexpectedly disabled\n");
190           exit (1);
191         }
192       if (key->subkeys->invalid)
193         {
194           fprintf (stderr, "Primary key unexpectedly invalid\n");
195           exit (1);
196         }
197       if (key->subkeys->can_encrypt)
198         {
199           fprintf (stderr, "Primary key unexpectedly usable for encryption\n");
200           exit (1);
201         }
202       if (!key->subkeys->can_sign)
203         {
204           fprintf (stderr, "Primary key unexpectedly unusable for signing\n");
205           exit (1);
206         }
207       if (!key->subkeys->can_certify)
208         {
209           fprintf (stderr, "Primary key unexpectedly unusable for certifications\n");
210           exit (1);
211         }
212       if (key->subkeys->secret)
213         {
214           fprintf (stderr, "Primary key unexpectedly secret\n");
215           exit (1);
216         }
217       if (key->subkeys->pubkey_algo != GPGME_PK_DSA)
218         {
219           fprintf (stderr, "Primary key has unexpected public key algo: %s\n",
220                    gpgme_pubkey_algo_name (key->subkeys->pubkey_algo));
221           exit (1);
222         }
223       if (key->subkeys->length != 1024)
224         {
225           fprintf (stderr, "Primary key has unexpected length: %i\n",
226                    key->subkeys->length);
227           exit (1);
228         }
229       if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16]))
230         {
231           fprintf (stderr, "Primary key has unexpected key ID: %s\n",
232                    key->subkeys->keyid);
233           exit (1);
234         }
235       if (strcmp (key->subkeys->fpr, keys[i].fpr))
236         {
237           fprintf (stderr, "Primary key has unexpected fingerprint: %s\n",
238                    key->subkeys->fpr);
239           exit (1);
240         }
241       if (key->subkeys->expires)
242         {
243           fprintf (stderr, "Primary key unexpectedly expires: %lu\n",
244                    key->subkeys->expires);
245           exit (1);
246         }
247
248       /* Secondary key.  */
249       if (key->subkeys->next->revoked)
250         {
251           fprintf (stderr, "Secondary key unexpectedly revoked\n");
252           exit (1);
253         }
254       if (key->subkeys->next->expired)
255         {
256           fprintf (stderr, "Secondary key unexpectedly expired\n");
257           exit (1);
258         }
259       if (key->subkeys->next->disabled)
260         {
261           fprintf (stderr, "Secondary key unexpectedly disabled\n");
262           exit (1);
263         }
264       if (key->subkeys->next->invalid)
265         {
266           fprintf (stderr, "Secondary key unexpectedly invalid\n");
267           exit (1);
268         }
269       if (!key->subkeys->next->can_encrypt)
270         {
271           fprintf (stderr, "Secondary key unexpectedly unusable for encryption\n");
272           exit (1);
273         }
274       if (key->subkeys->next->can_sign)
275         {
276           fprintf (stderr, "Secondary key unexpectedly usable for signing\n");
277           exit (1);
278         }
279       if (key->subkeys->next->can_certify)
280         {
281           fprintf (stderr, "Secondary key unexpectedly usable for certifications\n");
282           exit (1);
283         }
284       if (key->subkeys->next->secret)
285         {
286           fprintf (stderr, "Secondary key unexpectedly secret\n");
287           exit (1);
288         }
289       if (key->subkeys->next->pubkey_algo != GPGME_PK_ELG_E)
290         {
291           fprintf (stderr, "Secondary key has unexpected public key algo: %s\n",
292                    gpgme_pubkey_algo_name (key->subkeys->next->pubkey_algo));
293           exit (1);
294         }
295       if (key->subkeys->next->length != 1024)
296         {
297           fprintf (stderr, "Secondary key has unexpected length: %i\n",
298                    key->subkeys->next->length);
299           exit (1);
300         }
301       if (strcmp (key->subkeys->next->keyid, keys[i].sec_keyid))
302         {
303           fprintf (stderr, "Secondary key has unexpected key ID: %s\n",
304                    key->subkeys->next->keyid);
305           exit (1);
306         }
307       if (key->subkeys->next->fpr)
308         {
309           fprintf (stderr, "Secondary key has unexpectedly a fingerprint: %s\n",
310                    key->subkeys->next->fpr);
311           exit (1);
312         }
313       if (key->subkeys->next->expires)
314         {
315           fprintf (stderr, "Secondary key unexpectedly expires: %lu\n",
316                    key->subkeys->next->expires);
317           exit (1);
318         }
319
320       /* FIXME: The below test will crash if we want to check for a
321          name, comment or email that doesn't exist in the key's user
322          IDs.  */
323       if (!((!keys[i].uid[0].name && !key->uids)
324             || (keys[i].uid[0].name && !keys[i].uid[1].name
325                 && key->uids && !key->uids->next)
326             || (keys[i].uid[0].name && keys[i].uid[1].name
327                 && !keys[i].uid[2].name
328                 && key->uids && key->uids->next && !key->uids->next->next)
329             || (keys[i].uid[0].name && keys[i].uid[1].name
330                 && keys[i].uid[2].name
331                 && key->uids && key->uids->next && key->uids->next->next
332                 && !key->uids->next->next->next)))
333           {
334             fprintf (stderr, "Key has unexpected number of user IDs\n");
335             exit (1);
336           }
337       if (key->uids && key->uids->revoked)
338         {
339           fprintf (stderr, "First user ID unexpectedly revoked\n");
340           exit (1);
341         }
342       if (key->uids && key->uids->invalid)
343         {
344           fprintf (stderr, "First user ID unexpectedly invalid\n");
345           exit (1);
346         }
347       if (key->uids && key->uids->validity != GPGME_VALIDITY_UNKNOWN)
348         {
349           fprintf (stderr, "First user ID has unexpectedly validity: %i\n",
350                    key->uids->validity);
351           exit (1);
352         }
353       if (keys[i].uid[0].name
354           && strcmp (keys[i].uid[0].name, key->uids->name))
355         {
356           fprintf (stderr, "Unexpected name in first user ID: %s\n",
357                    key->uids->name);
358           exit (1);
359         }
360       if (keys[i].uid[0].comment
361           && strcmp (keys[i].uid[0].comment, key->uids->comment))
362         {
363           fprintf (stderr, "Unexpected comment in first user ID: %s\n",
364                    key->uids->comment);
365           exit (1);
366         }
367       if (keys[i].uid[0].email
368           && strcmp (keys[i].uid[0].email, key->uids->email))
369         {
370           fprintf (stderr, "Unexpected email in first user ID: %s\n",
371                    key->uids->email);
372           exit (1);
373         }
374       if (key->uids && (!key->uids->signatures || key->uids->signatures->next))
375         {
376           fprintf (stderr, "First user ID unexpected number of signatures\n");
377           exit (1);
378         }
379       if (keys[i].uid[0].sig.algo != key->uids->signatures->pubkey_algo)
380         {
381           fprintf (stderr, "Unexpected algorithm in first user ID sig: %s\n",
382                    gpgme_pubkey_algo_name (key->uids->signatures->pubkey_algo));
383           exit (1);
384         }
385       if (strcmp (keys[i].uid[0].sig.keyid, key->uids->signatures->keyid))
386         {
387           fprintf (stderr, "Unexpected key ID in first user ID sig: %s\n",
388                    key->uids->signatures->keyid);
389           exit (1);
390         }
391       if (strcmp (keys[i].uid[0].sig.name, key->uids->signatures->name))
392         {
393           fprintf (stderr, "Unexpected name in first user ID sig: %s\n",
394                    key->uids->signatures->name);
395           exit (1);
396         }
397       if (strcmp (keys[i].uid[0].sig.comment, key->uids->signatures->comment))
398         {
399           fprintf (stderr, "Unexpected comment in first user ID sig: %s\n",
400                    key->uids->signatures->comment);
401           exit (1);
402         }
403       if (strcmp (keys[i].uid[0].sig.email, key->uids->signatures->email))
404         {
405           fprintf (stderr, "Unexpected email in first user ID sig: %s\n",
406                    key->uids->signatures->email);
407           exit (1);
408         }
409       if (keys[i].uid[0].sig.class != key->uids->signatures->class)
410         {
411           fprintf (stderr, "Unexpected class in first user ID sig: %i\n",
412                    key->uids->signatures->class);
413           exit (1);
414         }
415       if (keys[i].uid[0].sig.exportable != key->uids->signatures->exportable)
416         {
417           fprintf (stderr, "Unexpected exportable stat in first user ID sig: %i\n",
418                    key->uids->signatures->exportable);
419           exit (1);
420         }
421
422       if (key->uids && key->uids->next && key->uids->next->revoked)
423         {
424           fprintf (stderr, "Second user ID unexpectedly revoked\n");
425           exit (1);
426         }
427       if (key->uids && key->uids->next && key->uids->next->invalid)
428         {
429           fprintf (stderr, "Second user ID unexpectedly invalid\n");
430           exit (1);
431         }
432       if (key->uids && key->uids->next
433           && key->uids->next->validity != GPGME_VALIDITY_UNKNOWN)
434         {
435           fprintf (stderr, "Second user ID has unexpectedly validity: %i\n",
436                    key->uids->next->validity);
437           exit (1);
438         }
439       if (keys[i].uid[1].name
440           && strcmp (keys[i].uid[1].name, key->uids->next->name))
441         {
442           fprintf (stderr, "Unexpected name in second user ID: %s\n",
443                    key->uids->next->name);
444           exit (1);
445         }
446       if (keys[i].uid[1].comment
447           && strcmp (keys[i].uid[1].comment, key->uids->next->comment))
448         {
449           fprintf (stderr, "Unexpected comment in second user ID: %s\n",
450                    key->uids->next->comment);
451           exit (1);
452         }
453       if (keys[i].uid[1].email
454           && strcmp (keys[i].uid[1].email, key->uids->next->email))
455         {
456           fprintf (stderr, "Unexpected email in second user ID: %s\n",
457                    key->uids->next->email);
458           exit (1);
459         }
460       if (key->uids && (!key->uids->next->signatures || key->uids->next->signatures->next))
461         {
462           fprintf (stderr, "Second user ID unexpected number of signatures\n");
463           exit (1);
464         }
465       if (keys[i].uid[1].sig.algo != key->uids->next->signatures->pubkey_algo)
466         {
467           fprintf (stderr, "Unexpected algorithm in second user ID sig: %s\n",
468                    gpgme_pubkey_algo_name (key->uids->next->signatures->pubkey_algo));
469           exit (1);
470         }
471       if (strcmp (keys[i].uid[1].sig.keyid, key->uids->next->signatures->keyid))
472         {
473           fprintf (stderr, "Unexpected key ID in second user ID sig: %s\n",
474                    key->uids->next->signatures->keyid);
475           exit (1);
476         }
477       if (strcmp (keys[i].uid[1].sig.name, key->uids->next->signatures->name))
478         {
479           fprintf (stderr, "Unexpected name in second user ID sig: %s\n",
480                    key->uids->next->signatures->name);
481           exit (1);
482         }
483       if (strcmp (keys[i].uid[1].sig.comment, key->uids->next->signatures->comment))
484         {
485           fprintf (stderr, "Unexpected comment in second user ID sig: %s\n",
486                    key->uids->next->signatures->comment);
487           exit (1);
488         }
489       if (strcmp (keys[i].uid[1].sig.email, key->uids->next->signatures->email))
490         {
491           fprintf (stderr, "Unexpected email in second user ID sig: %s\n",
492                    key->uids->next->signatures->email);
493           exit (1);
494         }
495       if (keys[i].uid[1].sig.class != key->uids->next->signatures->class)
496         {
497           fprintf (stderr, "Unexpected class in second user ID sig: %i\n",
498                    key->uids->next->signatures->class);
499           exit (1);
500         }
501       if (keys[i].uid[1].sig.exportable != key->uids->next->signatures->exportable)
502         {
503           fprintf (stderr, "Unexpected exportable stat in second user ID sig: %i\n",
504                    key->uids->next->signatures->exportable);
505           exit (1);
506         }
507
508       if (key->uids && key->uids->next && key->uids->next->next
509           && key->uids->next->next->revoked)
510         {
511           fprintf (stderr, "Third user ID unexpectedly revoked\n");
512           exit (1);
513         }
514       if (key->uids && key->uids->next && key->uids->next->next
515           && key->uids->next->next->invalid)
516         {
517           fprintf (stderr, "Third user ID unexpectedly invalid\n");
518           exit (1);
519         }
520       if (key->uids && key->uids->next && key->uids->next->next
521           && key->uids->next->next->validity != GPGME_VALIDITY_UNKNOWN)
522         {
523           fprintf (stderr, "Third user ID has unexpectedly validity: %i\n",
524                    key->uids->next->next->validity);
525           exit (1);
526         }
527       if (keys[i].uid[2].name
528           && strcmp (keys[i].uid[2].name, key->uids->next->next->name))
529         {
530           fprintf (stderr, "Unexpected name in third user ID: %s\n",
531                    key->uids->next->next->name);
532           exit (1);
533         }
534       if (keys[i].uid[2].comment
535           && strcmp (keys[i].uid[2].comment, key->uids->next->next->comment))
536         {
537           fprintf (stderr, "Unexpected comment in third user ID: %s\n",
538                    key->uids->next->next->comment);
539           exit (1);
540         }
541       if (keys[i].uid[2].email
542           && strcmp (keys[i].uid[2].email, key->uids->next->next->email))
543         {
544           fprintf (stderr, "Unexpected email in third user ID: %s\n",
545                    key->uids->next->next->email);
546           exit (1);
547         }
548       if (key->uids && (!key->uids->next->next->signatures
549                         || key->uids->next->next->signatures->next))
550         {
551           fprintf (stderr, "Third user ID unexpected number of signatures\n");
552           exit (1);
553         }
554       if (keys[i].uid[2].sig.algo != key->uids->next->next->signatures->pubkey_algo)
555         {
556           fprintf (stderr, "Unexpected algorithm in third user ID sig: %s\n",
557                    gpgme_pubkey_algo_name (key->uids->next->next->signatures->pubkey_algo));
558           exit (1);
559         }
560       if (strcmp (keys[i].uid[2].sig.keyid, key->uids->next->next->signatures->keyid))
561         {
562           fprintf (stderr, "Unexpected key ID in third user ID sig: %s\n",
563                    key->uids->next->next->signatures->keyid);
564           exit (1);
565         }
566       if (strcmp (keys[i].uid[2].sig.name, key->uids->next->next->signatures->name))
567         {
568           fprintf (stderr, "Unexpected name in third user ID sig: %s\n",
569                    key->uids->next->next->signatures->name);
570           exit (1);
571         }
572       if (strcmp (keys[i].uid[2].sig.comment, key->uids->next->next->signatures->comment))
573         {
574           fprintf (stderr, "Unexpected comment in third user ID sig: %s\n",
575                    key->uids->next->next->signatures->comment);
576           exit (1);
577         }
578       if (strcmp (keys[i].uid[2].sig.email, key->uids->next->next->signatures->email))
579         {
580           fprintf (stderr, "Unexpected email in third user ID sig: %s\n",
581                    key->uids->next->next->signatures->email);
582           exit (1);
583         }
584       if (keys[i].uid[2].sig.class != key->uids->next->next->signatures->class)
585         {
586           fprintf (stderr, "Unexpected class in third user ID sig: %i\n",
587                    key->uids->next->next->signatures->class);
588           exit (1);
589         }
590       if (keys[i].uid[2].sig.exportable != key->uids->next->next->signatures->exportable)
591         {
592           fprintf (stderr, "Unexpected exportable stat in third user ID sig: %i\n",
593                    key->uids->next->next->signatures->exportable);
594           exit (1);
595         }
596
597
598
599
600
601       gpgme_key_unref (key);
602       i++;
603     }
604   if (gpg_err_code (err) != GPG_ERR_EOF)
605     fail_if_err (err);
606   err = gpgme_op_keylist_end (ctx);
607   fail_if_err (err);
608
609   result = gpgme_op_keylist_result (ctx);
610   if (result->truncated)
611     {
612       fprintf (stderr, "Key listing unexpectedly truncated\n");
613       exit (1);
614     }
615
616   if (keys[i].fpr)
617     {
618       fprintf (stderr, "Less keys returned than expected\n");
619       exit (1);
620     }
621
622   gpgme_release (ctx);
623   return 0;
624 }