LCOV - code coverage report
Current view: top level - tests/gpg - t-import.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 57 100 57.0 %
Date: 2018-11-14 16:53:58 Functions: 2 2 100.0 %

          Line data    Source code
       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             : 
      38             : void
      39           2 : check_result (gpgme_import_result_t result, const char *fpr, int secret)
      40             : {
      41           2 :   if (result->considered != 1 && (secret && result->considered != 3))
      42             :     {
      43           0 :       fprintf (stderr, "Unexpected number of considered keys %i\n",
      44             :                result->considered);
      45           0 :       exit (1);
      46             :     }
      47           2 :   if (result->no_user_id != 0)
      48             :     {
      49           0 :       fprintf (stderr, "Unexpected number of user ids %i\n",
      50             :                result->no_user_id);
      51           0 :       exit (1);
      52             :     }
      53           2 :   if ((secret && result->imported != 0)
      54           2 :       || (!secret && (result->imported != 0 && result->imported != 1)))
      55             :     {
      56           0 :       fprintf (stderr, "Unexpected number of imported keys %i\n",
      57             :                result->imported);
      58           0 :       exit (1);
      59             :     }
      60           2 :   if (result->imported_rsa != 0)
      61             :     {
      62           0 :       fprintf (stderr, "Unexpected number of imported RSA keys %i\n",
      63             :                result->imported_rsa);
      64           0 :       exit (1);
      65             :     }
      66           2 :   if ((secret && (result->unchanged != 0 && result->unchanged != 1))
      67           2 :       || (!secret && ((result->imported == 0 && result->unchanged != 1)
      68           1 :                       || (result->imported == 1 && result->unchanged != 0))))
      69             :     {
      70           0 :       fprintf (stderr, "Unexpected number of unchanged keys %i\n",
      71             :                result->unchanged);
      72           0 :       exit (1);
      73             :     }
      74           2 :   if (result->new_user_ids != 0)
      75             :     {
      76           0 :       fprintf (stderr, "Unexpected number of new user IDs %i\n",
      77             :                result->new_user_ids);
      78           0 :       exit (1);
      79             :     }
      80           2 :   if (result->new_sub_keys != 0)
      81             :     {
      82           0 :       fprintf (stderr, "Unexpected number of new sub keys %i\n",
      83             :                result->new_sub_keys);
      84           0 :       exit (1);
      85             :     }
      86           2 :   if ((secret
      87           1 :        && ((result->secret_imported == 0 && result->new_signatures != 0)
      88           1 :            || (result->secret_imported == 1 && result->new_signatures > 1)))
      89           2 :       || (!secret && result->new_signatures != 0))
      90             :     {
      91           0 :       fprintf (stderr, "Unexpected number of new signatures %i\n",
      92             :                result->new_signatures);
      93           0 :       if (result->new_signatures == 2)
      94           0 :         fprintf (stderr, "### ignored due to gpg 1.3.4 problems\n");
      95             :       else
      96           0 :         exit (1);
      97             :     }
      98           2 :   if (result->new_revocations != 0)
      99             :     {
     100           0 :       fprintf (stderr, "Unexpected number of new revocations %i\n",
     101             :                result->new_revocations);
     102           0 :       exit (1);
     103             :     }
     104           2 :   if ((secret && result->secret_read != 1 && result->secret_read != 3)
     105           2 :       || (!secret && result->secret_read != 0))
     106             :     {
     107           0 :       fprintf (stderr, "Unexpected number of secret keys read %i\n",
     108             :                result->secret_read);
     109           0 :       exit (1);
     110             :     }
     111           2 :   if ((secret && result->secret_imported != 0 && result->secret_imported != 1
     112           0 :        && result->secret_imported != 2)
     113           2 :       || (!secret && result->secret_imported != 0))
     114             :     {
     115           0 :       fprintf (stderr, "Unexpected number of secret keys imported %i\n",
     116             :                result->secret_imported);
     117           0 :       exit (1);
     118             :     }
     119           2 :   if ((secret
     120           1 :        && ((result->secret_imported == 0 && result->secret_unchanged != 1
     121           0 :             && result->secret_unchanged != 2)
     122           1 :            || (result->secret_imported == 1 && result->secret_unchanged != 0)))
     123           2 :       || (!secret && result->secret_unchanged != 0))
     124             :     {
     125           0 :       fprintf (stderr, "Unexpected number of secret keys unchanged %i\n",
     126             :                result->secret_unchanged);
     127           0 :       exit (1);
     128             :     }
     129           2 :   if (result->not_imported != 0)
     130             :     {
     131           0 :       fprintf (stderr, "Unexpected number of secret keys not imported %i\n",
     132             :                result->not_imported);
     133           0 :       exit (1);
     134             :     }
     135           2 :   if (secret)
     136             :     {
     137           1 :       if (!result->imports
     138           1 :           || (result->imports->next && result->imports->next->next))
     139             :         {
     140           0 :           fprintf (stderr, "Unexpected number of status reports\n");
     141           0 :           exit (1);
     142             :         }
     143             :     }
     144             :   else
     145             :     {
     146           1 :       if (!result->imports || result->imports->next)
     147             :         {
     148           0 :           fprintf (stderr, "Unexpected number of status reports\n");
     149           0 :           exit (1);
     150             :         }
     151             :     }
     152           2 :   if (strcmp (fpr, result->imports->fpr))
     153             :     {
     154           0 :       fprintf (stderr, "Unexpected fingerprint %s\n",
     155           0 :                result->imports->fpr);
     156           0 :       exit (1);
     157             :     }
     158           2 :   if (result->imports->next && strcmp (fpr, result->imports->next->fpr))
     159             :     {
     160           0 :       fprintf (stderr, "Unexpected fingerprint on second status %s\n",
     161           0 :                result->imports->next->fpr);
     162           0 :       exit (1);
     163             :     }
     164           2 :   if (result->imports->result != 0)
     165             :     {
     166           0 :       fprintf (stderr, "Unexpected status result %s\n",
     167           0 :                gpgme_strerror (result->imports->result));
     168           0 :       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           2 : }
     208             : 
     209             : 
     210             : int
     211           1 : 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           1 :   char *pubkey_1_asc = make_filename ("pubkey-1.asc");
     218           1 :   char *seckey_1_asc = make_filename ("seckey-1.asc");
     219             : 
     220             :   (void)argc;
     221             :   (void)argv;
     222             : 
     223           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     224             : 
     225           1 :   err = gpgme_new (&ctx);
     226           1 :   fail_if_err (err);
     227             : 
     228           1 :   err = gpgme_data_new_from_file (&in, pubkey_1_asc, 1);
     229           1 :   free (pubkey_1_asc);
     230           1 :   fail_if_err (err);
     231             : 
     232           1 :   err = gpgme_op_import (ctx, in);
     233           1 :   fail_if_err (err);
     234           1 :   result = gpgme_op_import_result (ctx);
     235           1 :   check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 0);
     236           1 :   gpgme_data_release (in);
     237             : 
     238           1 :   err = gpgme_data_new_from_file (&in, seckey_1_asc, 1);
     239           1 :   free (seckey_1_asc);
     240           1 :   fail_if_err (err);
     241             : 
     242           1 :   err = gpgme_op_import (ctx, in);
     243           1 :   fail_if_err (err);
     244           1 :   result = gpgme_op_import_result (ctx);
     245           1 :   check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 1);
     246           1 :   gpgme_data_release (in);
     247             : 
     248           1 :   gpgme_release (ctx);
     249           1 :   return 0;
     250             : }

Generated by: LCOV version 1.13