LCOV - code coverage report
Current view: top level - tests/gpg - t-signers.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 71 88 80.7 %
Date: 2016-12-01 18:45:36 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* t-signers.c - Regression tests for the multiple signers interface.
       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 <unistd.h>
      32             : 
      33             : #include <gpgme.h>
      34             : 
      35             : #include "t-support.h"
      36             : 
      37             : 
      38             : static void
      39           3 : check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
      40             : {
      41             :   gpgme_new_signature_t signature;
      42             : 
      43           3 :   if (result->invalid_signers)
      44             :     {
      45           0 :       fprintf (stderr, "Invalid signer found: %s\n",
      46           0 :                result->invalid_signers->fpr);
      47           0 :       exit (1);
      48             :     }
      49           3 :   if (!result->signatures || !result->signatures->next
      50           3 :       || result->signatures->next->next)
      51             :     {
      52           0 :       fprintf (stderr, "Unexpected number of signatures created\n");
      53           0 :       exit (1);
      54             :     }
      55             : 
      56           3 :   signature = result->signatures;
      57          12 :   while (signature)
      58             :     {
      59           6 :       if (signature->type != type)
      60             :         {
      61           0 :           fprintf (stderr, "Wrong type of signature created\n");
      62           0 :           exit (1);
      63             :         }
      64           6 :       if (signature->pubkey_algo != GPGME_PK_DSA)
      65             :         {
      66           0 :           fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
      67           0 :                    signature->pubkey_algo);
      68           0 :           exit (1);
      69             :         }
      70           6 :       if (signature->hash_algo != GPGME_MD_SHA1)
      71             :         {
      72           0 :           fprintf (stderr, "Wrong hash algorithm reported: %i\n",
      73           0 :                    signature->hash_algo);
      74           0 :           exit (1);
      75             :         }
      76           6 :       if (signature->sig_class != 1)
      77             :         {
      78           0 :           fprintf (stderr, "Wrong signature class reported: %u\n",
      79             :                    signature->sig_class);
      80           0 :           exit (1);
      81             :         }
      82           6 :       if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
      83           6 :                    signature->fpr)
      84           3 :           && strcmp ("23FD347A419429BACCD5E72D6BC4778054ACD246",
      85           3 :                      signature->fpr))
      86             :         {
      87           0 :           fprintf (stderr, "Wrong fingerprint reported: %s\n",
      88             :                    signature->fpr);
      89           0 :           exit (1);
      90             :         }
      91           6 :       signature = signature->next;
      92             :     }
      93           3 : }
      94             : 
      95             : 
      96             : int
      97           1 : main (int argc, char *argv[])
      98             : {
      99             :   gpgme_ctx_t ctx;
     100             :   gpgme_error_t err;
     101             :   gpgme_data_t in, out;
     102             :   gpgme_key_t key[2];
     103             :   gpgme_sign_result_t result;
     104             :   char *agent_info;
     105             : 
     106             :   (void)argc;
     107             :   (void)argv;
     108             : 
     109           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     110             : 
     111           1 :   err = gpgme_new (&ctx);
     112           1 :   fail_if_err (err);
     113             : 
     114           1 :   agent_info = getenv("GPG_AGENT_INFO");
     115           1 :   if (!(agent_info && strchr (agent_info, ':')))
     116           1 :     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
     117             : 
     118           1 :   gpgme_set_textmode (ctx, 1);
     119           1 :   gpgme_set_armor (ctx, 1);
     120             : 
     121           1 :   err = gpgme_op_keylist_start (ctx, NULL, 1);
     122           1 :   fail_if_err (err);
     123           1 :   err = gpgme_op_keylist_next (ctx, &key[0]);
     124           1 :   fail_if_err (err);
     125           1 :   err = gpgme_op_keylist_next (ctx, &key[1]);
     126           1 :   fail_if_err (err);
     127           1 :   err = gpgme_op_keylist_end (ctx);
     128           1 :   fail_if_err (err);
     129             : 
     130           1 :   err = gpgme_signers_add (ctx, key[0]);
     131           1 :   fail_if_err (err);
     132           1 :   err = gpgme_signers_add (ctx, key[1]);
     133           1 :   fail_if_err (err);
     134             : 
     135           1 :   err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
     136           1 :   fail_if_err (err);
     137             : 
     138             :   /* First a normal signature.  */
     139           1 :   err = gpgme_data_new (&out);
     140           1 :   fail_if_err (err);
     141           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
     142           1 :   fail_if_err (err);
     143           1 :   result = gpgme_op_sign_result (ctx);
     144           1 :   check_result (result, GPGME_SIG_MODE_NORMAL);
     145           1 :   print_data (out);
     146           1 :   gpgme_data_release (out);
     147             : 
     148             :   /* Now a detached signature.  */
     149           1 :   gpgme_data_seek (in, 0, SEEK_SET);
     150           1 :   err = gpgme_data_new (&out);
     151           1 :   fail_if_err (err);
     152           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH);
     153           1 :   fail_if_err (err);
     154           1 :   result = gpgme_op_sign_result (ctx);
     155           1 :   check_result (result, GPGME_SIG_MODE_DETACH);
     156           1 :   print_data (out);
     157           1 :   gpgme_data_release (out);
     158             : 
     159             :   /* And finally a cleartext signature.  */
     160           1 :   gpgme_data_seek (in, 0, SEEK_SET);
     161           1 :   err = gpgme_data_new (&out);
     162           1 :   fail_if_err (err);
     163           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR);
     164           1 :   fail_if_err (err);
     165           1 :   result = gpgme_op_sign_result (ctx);
     166           1 :   check_result (result, GPGME_SIG_MODE_CLEAR);
     167           1 :   print_data (out);
     168           1 :   gpgme_data_release (out);
     169           1 :   gpgme_data_seek (in, 0, SEEK_SET);
     170             : 
     171           1 :   gpgme_data_release (in);
     172           1 :   gpgme_release (ctx);
     173             : 
     174           1 :   gpgme_key_unref (key[0]);
     175           1 :   gpgme_key_unref (key[1]);
     176           1 :   return 0;
     177             : }

Generated by: LCOV version 1.11