LCOV - code coverage report
Current view: top level - tests/gpgsm - t-keylist.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 68 186 36.6 %
Date: 2015-11-05 17:14:26 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             :   char *fpr;
      40             :   int secret;
      41             :   long timestamp;
      42             :   long expires;
      43             :   char *issuer_serial;
      44             :   char *issuer_name;
      45             :   char *chain_id;
      46             :   char *uid;
      47             :   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 (int argc, char **argv)
      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 :           continue;
     114             :         }
     115             :       else
     116           3 :         printf ("Checking key %s\n", key->subkeys->fpr);
     117             : 
     118             :       /* Global key flags.  */
     119           3 :       if (key->revoked)
     120             :         {
     121           0 :           fprintf (stderr, "Key unexpectedly revoked\n");
     122           0 :           exit (1);
     123             :         }
     124           3 :       if (key->expired)
     125             :         {
     126           0 :           fprintf (stderr, "Key unexpectedly expired\n");
     127           0 :           exit (1);
     128             :         }
     129           3 :       if (key->disabled)
     130             :         {
     131           0 :           fprintf (stderr, "Key unexpectedly disabled\n");
     132           0 :           exit (1);
     133             :         }
     134           3 :       if (key->invalid)
     135             :         {
     136           0 :           fprintf (stderr, "Key unexpectedly invalid\n");
     137           0 :           exit (1);
     138             :         }
     139           3 :       if (key->can_encrypt != keys[i].secret)
     140             :         {
     141           0 :           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
     142           0 :                    key->can_encrypt ? "" : " not");
     143           0 :           exit (1);
     144             :         }
     145           3 :       if (key->can_sign != keys[i].secret)
     146             :         {
     147           0 :           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
     148           0 :                    key->can_sign ? "" : " not");
     149           0 :           exit (1);
     150             :         }
     151           3 :       if (!key->can_certify)
     152             :         {
     153           0 :           fprintf (stderr, "Key unexpectedly unusable for certifications\n");
     154           0 :           exit (1);
     155             :         }
     156           3 :       if (key->secret != keys[i].secret)
     157             :         {
     158           0 :           fprintf (stderr, "Key unexpectedly%s secret\n",
     159           0 :                    key->secret ? "" : " not");
     160           0 :           exit (1);
     161             :         }
     162           3 :       if (key->protocol != GPGME_PROTOCOL_CMS)
     163             :         {
     164           0 :           fprintf (stderr, "Key has unexpected protocol: %s\n",
     165           0 :                    gpgme_get_protocol_name (key->protocol));
     166           0 :           exit (1);
     167             :         }
     168           3 :       if (!key->issuer_serial)
     169             :         {
     170           0 :           fprintf (stderr, "Key unexpectedly misses issuer serial\n");
     171           0 :           exit (1);
     172             :         }
     173           3 :       if (strcmp (key->issuer_serial, keys[i].issuer_serial))
     174             :         {
     175           0 :           fprintf (stderr, "Key has unexpected issuer serial: %s\n",
     176           0 :                    key->issuer_serial);
     177           0 :           exit (1);
     178             :         }
     179           3 :       if (!key->issuer_name)
     180             :         {
     181           0 :           fprintf (stderr, "Key unexpectedly misses issuer name\n");
     182           0 :           exit (1);
     183             :         }
     184           3 :       if (strcmp (key->issuer_name, keys[i].issuer_name))
     185             :         {
     186           0 :           fprintf (stderr, "Key has unexpected issuer name: %s\n",
     187           0 :                    key->issuer_name);
     188           0 :           exit (1);
     189             :         }
     190           3 :       if (key->chain_id && !keys[i].chain_id)
     191             :         {
     192           0 :           fprintf (stderr, "Key unexpectedly carries chain ID: %s\n",
     193           0 :                    key->chain_id);
     194           0 :           exit (1);
     195             :         }
     196           3 :       if (!key->chain_id && keys[i].chain_id)
     197             :         {
     198           0 :           fprintf (stderr, "Key unexpectedly carries no chain ID\n");
     199           0 :           exit (1);
     200             :         }
     201           3 :       if (key->chain_id && strcmp (key->chain_id, keys[i].chain_id))
     202             :         {
     203           0 :           fprintf (stderr, "Key carries unexpected chain ID: %s\n",
     204           0 :                    key->chain_id);
     205           0 :           exit (1);
     206             :         }
     207           3 :       if (key->owner_trust != GPGME_VALIDITY_UNKNOWN)
     208             :         {
     209           0 :           fprintf (stderr, "Key has unexpected owner trust: %i\n",
     210           0 :                    key->owner_trust);
     211           0 :           exit (1);
     212             :         }
     213           3 :       if (!key->subkeys || key->subkeys->next)
     214             :         {
     215           0 :           fprintf (stderr, "Key has unexpected number of subkeys\n");
     216           0 :           exit (1);
     217             :         }
     218             : 
     219             :       /* Primary key.  */
     220           3 :       if (key->subkeys->revoked)
     221             :         {
     222           0 :           fprintf (stderr, "Primary key unexpectedly revoked\n");
     223           0 :           exit (1);
     224             :         }
     225           3 :       if (key->subkeys->expired)
     226             :         {
     227           0 :           fprintf (stderr, "Primary key unexpectedly expired\n");
     228           0 :           exit (1);
     229             :         }
     230           3 :       if (key->subkeys->disabled)
     231             :         {
     232           0 :           fprintf (stderr, "Primary key unexpectedly disabled\n");
     233           0 :           exit (1);
     234             :         }
     235           3 :       if (key->subkeys->invalid)
     236             :         {
     237           0 :           fprintf (stderr, "Primary key unexpectedly invalid\n");
     238           0 :           exit (1);
     239             :         }
     240           3 :       if (key->subkeys->can_encrypt != keys[i].secret)
     241             :         {
     242           0 :           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
     243           0 :                    key->subkeys->can_encrypt ? "" : " not");
     244           0 :           exit (1);
     245             :         }
     246           3 :       if (key->subkeys->can_sign != keys[i].secret)
     247             :         {
     248           0 :           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
     249           0 :                    key->subkeys->can_sign ? "" : " not");
     250           0 :           exit (1);
     251             :         }
     252           3 :       if (!key->subkeys->can_certify)
     253             :         {
     254           0 :           fprintf (stderr, "Primary key unexpectedly unusable for certifications\n");
     255           0 :           exit (1);
     256             :         }
     257           3 :       if (key->subkeys->secret != keys[i].secret)
     258             :         {
     259           0 :           fprintf (stderr, "Primary Key unexpectedly%s secret\n",
     260           0 :                    key->secret ? "" : " not");
     261           0 :           exit (1);
     262             :         }
     263           3 :       if (key->subkeys->pubkey_algo != GPGME_PK_RSA)
     264             :         {
     265           0 :           fprintf (stderr, "Primary key has unexpected public key algo: %s\n",
     266           0 :                    gpgme_pubkey_algo_name (key->subkeys->pubkey_algo));
     267           0 :           exit (1);
     268             :         }
     269           3 :       if (key->subkeys->length != keys[i].key_length)
     270             :         {
     271           0 :           fprintf (stderr, "Primary key has unexpected length: %i\n",
     272           0 :                    key->subkeys->length);
     273           0 :           exit (1);
     274             :         }
     275           3 :       if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16]))
     276             :         {
     277           0 :           fprintf (stderr, "Primary key has unexpected key ID: %s\n",
     278           0 :                    key->subkeys->keyid);
     279           0 :           exit (1);
     280             :         }
     281           3 :       if (strcmp (key->subkeys->fpr, keys[i].fpr))
     282             :         {
     283           0 :           fprintf (stderr, "Primary key has unexpected fingerprint: %s\n",
     284           0 :                    key->subkeys->fpr);
     285           0 :           exit (1);
     286             :         }
     287           3 :       if (key->subkeys->timestamp != keys[i].timestamp)
     288             :         {
     289           0 :           fprintf (stderr, "Primary key unexpected timestamp: %lu\n",
     290           0 :                    key->subkeys->timestamp);
     291           0 :           exit (1);
     292             :         }
     293           3 :       if (key->subkeys->expires != keys[i].expires)
     294             :         {
     295           0 :           fprintf (stderr, "Primary key unexpectedly expires: %lu\n",
     296           0 :                    key->subkeys->expires);
     297           0 :           exit (1);
     298             :         }
     299             : 
     300             :       /* Be tolerant against a missing email (ie, older gpgsm versions).  */
     301           3 :       if (!key->uids || (key->uids->next && !keys[i].email))
     302             :         {
     303           0 :           fprintf (stderr, "Key has unexpected number of user IDs\n");
     304           0 :           exit (1);
     305             :         }
     306           3 :       if (key->uids->revoked)
     307             :         {
     308           0 :           fprintf (stderr, "User ID unexpectedly revoked\n");
     309           0 :           exit (1);
     310             :         }
     311           3 :       if (key->uids->invalid)
     312             :         {
     313           0 :           fprintf (stderr, "User ID unexpectedly invalid\n");
     314           0 :           exit (1);
     315             :         }
     316           3 :       if (key->uids->validity != keys[i].validity)
     317             :         {
     318           0 :           fprintf (stderr, "User ID unexpectedly validity: %i\n",
     319           0 :                    key->uids->validity);
     320           0 :           exit (1);
     321             :         }
     322           3 :       if (key->uids->signatures)
     323             :         {
     324           0 :           fprintf (stderr, "User ID unexpectedly signed\n");
     325           0 :           exit (1);
     326             :         }
     327           3 :       if (!key->uids->name || key->uids->name[0])
     328             :         {
     329           0 :           fprintf (stderr, "Unexpected name in user ID: %s\n",
     330           0 :                    key->uids->name);
     331           0 :           exit (1);
     332             :         }
     333           3 :       if (!key->uids->comment || key->uids->comment[0])
     334             :         {
     335           0 :           fprintf (stderr, "Unexpected comment in user ID: %s\n",
     336           0 :                    key->uids->comment);
     337           0 :           exit (1);
     338             :         }
     339           3 :       if (!key->uids->email || key->uids->email[0])
     340             :         {
     341           0 :           fprintf (stderr, "Unexpected email in user ID: %s\n",
     342           0 :                    key->uids->email);
     343           0 :           exit (1);
     344             :         }
     345           3 :       if (!key->uids->uid || strcmp (key->uids->uid, keys[i].uid))
     346             :         {
     347           0 :           fprintf (stderr, "Unexpected uid in user ID: %s\n",
     348           0 :                    key->uids->uid);
     349           0 :           exit (1);
     350             :         }
     351           3 :       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
     352             :         {
     353           0 :           fprintf (stderr, "Unexpected email in user ID: %s\n",
     354           0 :                    key->uids->next->uid);
     355           0 :           exit (1);
     356             :         }
     357           3 :       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
     358             :         {
     359           0 :           fprintf (stderr, "Unexpected email in user ID: %s\n",
     360           0 :                    key->uids->next->uid);
     361           0 :           exit (1);
     362             :         }
     363             : 
     364             : 
     365             : 
     366           3 :       gpgme_key_unref (key);
     367           3 :       i++;
     368             :     }
     369           1 :   if (gpgme_err_code (err) != GPG_ERR_EOF)
     370           0 :     fail_if_err (err);
     371           1 :   err = gpgme_op_keylist_end (ctx);
     372           1 :   fail_if_err (err);
     373             : 
     374           1 :   result = gpgme_op_keylist_result (ctx);
     375           1 :   if (result->truncated)
     376             :     {
     377           0 :       fprintf (stderr, "Key listing unexpectedly truncated\n");
     378           0 :       exit (1);
     379             :     }
     380             : 
     381           1 :   if (keys[i].fpr)
     382             :     {
     383           0 :       fprintf (stderr, "Less keys returned than expected\n");
     384           0 :       exit (1);
     385             :     }
     386             : 
     387           1 :   gpgme_release (ctx);
     388           1 :   return 0;
     389             : }

Generated by: LCOV version 1.11