LCOV - code coverage report
Current view: top level - tests/gpgsm - t-keylist.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 69 187 36.9 %
Date: 2016-11-29 15:07:43 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* t-keylist.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             : 
      32             : #include <gpgme.h>
      33             : 
      34             : #include "t-support.h"
      35             : 
      36             : 
      37             : struct
      38             : {
      39             :   const char *fpr;
      40             :   int secret;
      41             :   long timestamp;
      42             :   long expires;
      43             :   const char *issuer_serial;
      44             :   const char *issuer_name;
      45             :   const char *chain_id;
      46             :   const char *uid;
      47             :   const char *email;
      48             :   gpgme_validity_t validity;
      49             :   unsigned int key_length;
      50             : }
      51             : keys[] =
      52             :   {
      53             :     { "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", 1, 1007372198, 1038908198, "00",
      54             :       "CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=D\xc3\xbcsseldorf,C=DE",
      55             :       "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
      56             :       "CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=D\xc3\xbcsseldorf,C=DE",
      57             :       NULL, GPGME_VALIDITY_ULTIMATE, 1024
      58             :     },
      59             :     { "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", 0, 909684190, 1009821790, "01",
      60             :       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
      61             :       "CN=DFN Top Level Certification Authority,OU=DFN-PCA,"
      62             :       "O=Deutsches Forschungsnetz,C=DE",
      63             :       "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD",
      64             :       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
      65             :       "CN=DFN Top Level Certification Authority,OU=DFN-PCA,"
      66             :       "O=Deutsches Forschungsnetz,C=DE",
      67             :       "<certify@pca.dfn.de>", GPGME_VALIDITY_NEVER, 2048
      68             :     },
      69             :     { "2C8F3C356AB761CB3674835B792CDA52937F9285", 0, 973183644, 1009735200, "15",
      70             :       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
      71             :       "CN=DFN Top Level Certification Authority,OU=DFN-PCA,"
      72             :       "O=Deutsches Forschungsnetz,C=DE",
      73             :       "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD",
      74             :       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
      75             :       "CN=DFN Server Certification Authority,OU=DFN-PCA,"
      76             :       "O=Deutsches Forschungsnetz,C=DE",
      77             :       "<certify@pca.dfn.de>", GPGME_VALIDITY_UNKNOWN, 2048
      78             :     },
      79             :     { NULL }
      80             :   };
      81             : 
      82             : 
      83             : int
      84           1 : main (void)
      85             : {
      86             :   gpgme_error_t err;
      87             :   gpgme_ctx_t ctx;
      88             :   gpgme_key_t key;
      89             :   gpgme_keylist_result_t result;
      90           1 :   int i = 0;
      91             : 
      92           1 :   init_gpgme (GPGME_PROTOCOL_CMS);
      93             : 
      94           1 :   err = gpgme_new (&ctx);
      95           1 :   fail_if_err (err);
      96           1 :   gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
      97             : 
      98           1 :   err = gpgme_op_keylist_start (ctx, NULL, 0);
      99           1 :   fail_if_err (err);
     100             : 
     101           7 :   while (!(err = gpgme_op_keylist_next (ctx, &key)))
     102             :     {
     103           5 :       if (!keys[i].fpr)
     104             :         {
     105           0 :           fprintf (stderr, "More keys returned than expected\n");
     106           0 :           exit (1);
     107             :         }
     108             : 
     109           5 :       if (strcmp (key->subkeys->fpr, keys[i].fpr))
     110             :         {
     111           2 :           fprintf (stderr, "Warning: Skipping unknown key %s\n",
     112           2 :                    key->subkeys->fpr);
     113           2 :           gpgme_key_unref (key);
     114           2 :           continue;
     115             :         }
     116             :       else
     117           3 :         printf ("Checking key %s\n", key->subkeys->fpr);
     118             : 
     119             :       /* Global key flags.  */
     120           3 :       if (key->revoked)
     121             :         {
     122           0 :           fprintf (stderr, "Key unexpectedly revoked\n");
     123           0 :           exit (1);
     124             :         }
     125           3 :       if (key->expired)
     126             :         {
     127           0 :           fprintf (stderr, "Key unexpectedly expired\n");
     128           0 :           exit (1);
     129             :         }
     130           3 :       if (key->disabled)
     131             :         {
     132           0 :           fprintf (stderr, "Key unexpectedly disabled\n");
     133           0 :           exit (1);
     134             :         }
     135           3 :       if (key->invalid)
     136             :         {
     137           0 :           fprintf (stderr, "Key unexpectedly invalid\n");
     138           0 :           exit (1);
     139             :         }
     140           3 :       if (key->can_encrypt != keys[i].secret)
     141             :         {
     142           0 :           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
     143           0 :                    key->can_encrypt ? "" : " not");
     144           0 :           exit (1);
     145             :         }
     146           3 :       if (key->can_sign != keys[i].secret)
     147             :         {
     148           0 :           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
     149           0 :                    key->can_sign ? "" : " not");
     150           0 :           exit (1);
     151             :         }
     152           3 :       if (!key->can_certify)
     153             :         {
     154           0 :           fprintf (stderr, "Key unexpectedly unusable for certifications\n");
     155           0 :           exit (1);
     156             :         }
     157           3 :       if (key->secret != keys[i].secret)
     158             :         {
     159           0 :           fprintf (stderr, "Key unexpectedly%s secret\n",
     160           0 :                    key->secret ? "" : " not");
     161           0 :           exit (1);
     162             :         }
     163           3 :       if (key->protocol != GPGME_PROTOCOL_CMS)
     164             :         {
     165           0 :           fprintf (stderr, "Key has unexpected protocol: %s\n",
     166           0 :                    gpgme_get_protocol_name (key->protocol));
     167           0 :           exit (1);
     168             :         }
     169           3 :       if (!key->issuer_serial)
     170             :         {
     171           0 :           fprintf (stderr, "Key unexpectedly misses issuer serial\n");
     172           0 :           exit (1);
     173             :         }
     174           3 :       if (strcmp (key->issuer_serial, keys[i].issuer_serial))
     175             :         {
     176           0 :           fprintf (stderr, "Key has unexpected issuer serial: %s\n",
     177           0 :                    key->issuer_serial);
     178           0 :           exit (1);
     179             :         }
     180           3 :       if (!key->issuer_name)
     181             :         {
     182           0 :           fprintf (stderr, "Key unexpectedly misses issuer name\n");
     183           0 :           exit (1);
     184             :         }
     185           3 :       if (strcmp (key->issuer_name, keys[i].issuer_name))
     186             :         {
     187           0 :           fprintf (stderr, "Key has unexpected issuer name: %s\n",
     188           0 :                    key->issuer_name);
     189           0 :           exit (1);
     190             :         }
     191           3 :       if (key->chain_id && !keys[i].chain_id)
     192             :         {
     193           0 :           fprintf (stderr, "Key unexpectedly carries chain ID: %s\n",
     194           0 :                    key->chain_id);
     195           0 :           exit (1);
     196             :         }
     197           3 :       if (!key->chain_id && keys[i].chain_id)
     198             :         {
     199           0 :           fprintf (stderr, "Key unexpectedly carries no chain ID\n");
     200           0 :           exit (1);
     201             :         }
     202           3 :       if (key->chain_id && strcmp (key->chain_id, keys[i].chain_id))
     203             :         {
     204           0 :           fprintf (stderr, "Key carries unexpected chain ID: %s\n",
     205           0 :                    key->chain_id);
     206           0 :           exit (1);
     207             :         }
     208           3 :       if (key->owner_trust != GPGME_VALIDITY_UNKNOWN)
     209             :         {
     210           0 :           fprintf (stderr, "Key has unexpected owner trust: %i\n",
     211           0 :                    key->owner_trust);
     212           0 :           exit (1);
     213             :         }
     214           3 :       if (!key->subkeys || key->subkeys->next)
     215             :         {
     216           0 :           fprintf (stderr, "Key has unexpected number of subkeys\n");
     217           0 :           exit (1);
     218             :         }
     219             : 
     220             :       /* Primary key.  */
     221           3 :       if (key->subkeys->revoked)
     222             :         {
     223           0 :           fprintf (stderr, "Primary key unexpectedly revoked\n");
     224           0 :           exit (1);
     225             :         }
     226           3 :       if (key->subkeys->expired)
     227             :         {
     228           0 :           fprintf (stderr, "Primary key unexpectedly expired\n");
     229           0 :           exit (1);
     230             :         }
     231           3 :       if (key->subkeys->disabled)
     232             :         {
     233           0 :           fprintf (stderr, "Primary key unexpectedly disabled\n");
     234           0 :           exit (1);
     235             :         }
     236           3 :       if (key->subkeys->invalid)
     237             :         {
     238           0 :           fprintf (stderr, "Primary key unexpectedly invalid\n");
     239           0 :           exit (1);
     240             :         }
     241           3 :       if (key->subkeys->can_encrypt != keys[i].secret)
     242             :         {
     243           0 :           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
     244           0 :                    key->subkeys->can_encrypt ? "" : " not");
     245           0 :           exit (1);
     246             :         }
     247           3 :       if (key->subkeys->can_sign != keys[i].secret)
     248             :         {
     249           0 :           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
     250           0 :                    key->subkeys->can_sign ? "" : " not");
     251           0 :           exit (1);
     252             :         }
     253           3 :       if (!key->subkeys->can_certify)
     254             :         {
     255           0 :           fprintf (stderr, "Primary key unexpectedly unusable for certifications\n");
     256           0 :           exit (1);
     257             :         }
     258           3 :       if (key->subkeys->secret != keys[i].secret)
     259             :         {
     260           0 :           fprintf (stderr, "Primary Key unexpectedly%s secret\n",
     261           0 :                    key->secret ? "" : " not");
     262           0 :           exit (1);
     263             :         }
     264           3 :       if (key->subkeys->pubkey_algo != GPGME_PK_RSA)
     265             :         {
     266           0 :           fprintf (stderr, "Primary key has unexpected public key algo: %s\n",
     267           0 :                    gpgme_pubkey_algo_name (key->subkeys->pubkey_algo));
     268           0 :           exit (1);
     269             :         }
     270           3 :       if (key->subkeys->length != keys[i].key_length)
     271             :         {
     272           0 :           fprintf (stderr, "Primary key has unexpected length: %i\n",
     273           0 :                    key->subkeys->length);
     274           0 :           exit (1);
     275             :         }
     276           3 :       if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16]))
     277             :         {
     278           0 :           fprintf (stderr, "Primary key has unexpected key ID: %s\n",
     279           0 :                    key->subkeys->keyid);
     280           0 :           exit (1);
     281             :         }
     282           3 :       if (strcmp (key->subkeys->fpr, keys[i].fpr))
     283             :         {
     284           0 :           fprintf (stderr, "Primary key has unexpected fingerprint: %s\n",
     285           0 :                    key->subkeys->fpr);
     286           0 :           exit (1);
     287             :         }
     288           3 :       if (key->subkeys->timestamp != keys[i].timestamp)
     289             :         {
     290           0 :           fprintf (stderr, "Primary key unexpected timestamp: %lu\n",
     291           0 :                    key->subkeys->timestamp);
     292           0 :           exit (1);
     293             :         }
     294           3 :       if (key->subkeys->expires != keys[i].expires)
     295             :         {
     296           0 :           fprintf (stderr, "Primary key unexpectedly expires: %lu\n",
     297           0 :                    key->subkeys->expires);
     298           0 :           exit (1);
     299             :         }
     300             : 
     301             :       /* Be tolerant against a missing email (ie, older gpgsm versions).  */
     302           3 :       if (!key->uids || (key->uids->next && !keys[i].email))
     303             :         {
     304           0 :           fprintf (stderr, "Key has unexpected number of user IDs\n");
     305           0 :           exit (1);
     306             :         }
     307           3 :       if (key->uids->revoked)
     308             :         {
     309           0 :           fprintf (stderr, "User ID unexpectedly revoked\n");
     310           0 :           exit (1);
     311             :         }
     312           3 :       if (key->uids->invalid)
     313             :         {
     314           0 :           fprintf (stderr, "User ID unexpectedly invalid\n");
     315           0 :           exit (1);
     316             :         }
     317           3 :       if (key->uids->validity != keys[i].validity)
     318             :         {
     319           0 :           fprintf (stderr, "User ID unexpectedly validity: %i\n",
     320           0 :                    key->uids->validity);
     321           0 :           exit (1);
     322             :         }
     323           3 :       if (key->uids->signatures)
     324             :         {
     325           0 :           fprintf (stderr, "User ID unexpectedly signed\n");
     326           0 :           exit (1);
     327             :         }
     328           3 :       if (!key->uids->name || key->uids->name[0])
     329             :         {
     330           0 :           fprintf (stderr, "Unexpected name in user ID: %s\n",
     331           0 :                    key->uids->name);
     332           0 :           exit (1);
     333             :         }
     334           3 :       if (!key->uids->comment || key->uids->comment[0])
     335             :         {
     336           0 :           fprintf (stderr, "Unexpected comment in user ID: %s\n",
     337           0 :                    key->uids->comment);
     338           0 :           exit (1);
     339             :         }
     340           3 :       if (!key->uids->email || key->uids->email[0])
     341             :         {
     342           0 :           fprintf (stderr, "Unexpected email in user ID: %s\n",
     343           0 :                    key->uids->email);
     344           0 :           exit (1);
     345             :         }
     346           3 :       if (!key->uids->uid || strcmp (key->uids->uid, keys[i].uid))
     347             :         {
     348           0 :           fprintf (stderr, "Unexpected uid in user ID: %s\n",
     349           0 :                    key->uids->uid);
     350           0 :           exit (1);
     351             :         }
     352           3 :       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
     353             :         {
     354           0 :           fprintf (stderr, "Unexpected email in user ID: %s\n",
     355           0 :                    key->uids->next->uid);
     356           0 :           exit (1);
     357             :         }
     358           3 :       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
     359             :         {
     360           0 :           fprintf (stderr, "Unexpected email in user ID: %s\n",
     361           0 :                    key->uids->next->uid);
     362           0 :           exit (1);
     363             :         }
     364             : 
     365             : 
     366             : 
     367           3 :       gpgme_key_unref (key);
     368           3 :       i++;
     369             :     }
     370           1 :   if (gpgme_err_code (err) != GPG_ERR_EOF)
     371           0 :     fail_if_err (err);
     372           1 :   err = gpgme_op_keylist_end (ctx);
     373           1 :   fail_if_err (err);
     374             : 
     375           1 :   result = gpgme_op_keylist_result (ctx);
     376           1 :   if (result->truncated)
     377             :     {
     378           0 :       fprintf (stderr, "Key listing unexpectedly truncated\n");
     379           0 :       exit (1);
     380             :     }
     381             : 
     382           1 :   if (keys[i].fpr)
     383             :     {
     384           0 :       fprintf (stderr, "Less keys returned than expected\n");
     385           0 :       exit (1);
     386             :     }
     387             : 
     388           1 :   gpgme_release (ctx);
     389           1 :   return 0;
     390             : }

Generated by: LCOV version 1.11