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

          Line data    Source code
       1             : /* t-sign.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 <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           3 :   if (result->invalid_signers)
      42             :     {
      43           0 :       fprintf (stderr, "Invalid signer found: %s\n",
      44           0 :                result->invalid_signers->fpr);
      45           0 :       exit (1);
      46             :     }
      47           3 :   if (!result->signatures || result->signatures->next)
      48             :     {
      49           0 :       fprintf (stderr, "Unexpected number of signatures created\n");
      50           0 :       exit (1);
      51             :     }
      52           3 :   if (result->signatures->type != type)
      53             :     {
      54           0 :       fprintf (stderr, "Wrong type of signature created\n");
      55           0 :       exit (1);
      56             :     }
      57           3 :   if (result->signatures->pubkey_algo != GPGME_PK_DSA)
      58             :     {
      59           0 :       fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
      60           0 :                result->signatures->pubkey_algo);
      61           0 :       exit (1);
      62             :     }
      63           3 :   if (result->signatures->hash_algo != GPGME_MD_SHA1)
      64             :     {
      65           0 :       fprintf (stderr, "Wrong hash algorithm reported: %i\n",
      66           0 :                result->signatures->hash_algo);
      67           0 :       exit (1);
      68             :     }
      69           3 :   if (result->signatures->sig_class != 1)
      70             :     {
      71           0 :       fprintf (stderr, "Wrong signature class reported: %u\n",
      72           0 :                result->signatures->sig_class);
      73           0 :       exit (1);
      74             :     }
      75           3 :   if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
      76           3 :               result->signatures->fpr))
      77             :     {
      78           0 :       fprintf (stderr, "Wrong fingerprint reported: %s\n",
      79           0 :                result->signatures->fpr);
      80           0 :       exit (1);
      81             :     }
      82           3 : }
      83             : 
      84             : 
      85             : int
      86           1 : main (int argc, char **argv)
      87             : {
      88             :   gpgme_ctx_t ctx;
      89             :   gpgme_error_t err;
      90             :   gpgme_data_t in, out;
      91             :   gpgme_sign_result_t result;
      92             :   char *agent_info;
      93             : 
      94             :   (void)argc;
      95             :   (void)argv;
      96             : 
      97           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
      98             : 
      99           1 :   err = gpgme_new (&ctx);
     100           1 :   fail_if_err (err);
     101             : 
     102           1 :   agent_info = getenv ("GPG_AGENT_INFO");
     103           1 :   if (!(agent_info && strchr (agent_info, ':')))
     104           1 :     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
     105             : 
     106           1 :   gpgme_set_textmode (ctx, 1);
     107           1 :   gpgme_set_armor (ctx, 1);
     108             : 
     109             : #if 0
     110             :   {
     111             :     gpgme_key_t akey;
     112             :     err = gpgme_get_key (ctx, "0x68697734", &akey, 0);
     113             :     fail_if_err (err);
     114             :     err = gpgme_signers_add (ctx, akey);
     115             :     fail_if_err (err);
     116             :     gpgme_key_unref (akey);
     117             :   }
     118             : #endif
     119             : 
     120           1 :   err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
     121           1 :   fail_if_err (err);
     122             : 
     123             :   /* First a normal signature.  */
     124           1 :   err = gpgme_data_new (&out);
     125           1 :   fail_if_err (err);
     126           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
     127           1 :   fail_if_err (err);
     128           1 :   result = gpgme_op_sign_result (ctx);
     129           1 :   check_result (result, GPGME_SIG_MODE_NORMAL);
     130           1 :   print_data (out);
     131           1 :   gpgme_data_release (out);
     132             : 
     133             :   /* Now a detached signature.  */
     134           1 :   gpgme_data_seek (in, 0, SEEK_SET);
     135           1 :   err = gpgme_data_new (&out);
     136           1 :   fail_if_err (err);
     137           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH);
     138           1 :   fail_if_err (err);
     139           1 :   result = gpgme_op_sign_result (ctx);
     140           1 :   check_result (result, GPGME_SIG_MODE_DETACH);
     141           1 :   print_data (out);
     142           1 :   gpgme_data_release (out);
     143             : 
     144             :   /* And finally a cleartext signature.  */
     145           1 :   gpgme_data_seek (in, 0, SEEK_SET);
     146           1 :   err = gpgme_data_new (&out);
     147           1 :   fail_if_err (err);
     148           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR);
     149           1 :   fail_if_err (err);
     150           1 :   result = gpgme_op_sign_result (ctx);
     151           1 :   check_result (result, GPGME_SIG_MODE_CLEAR);
     152           1 :   print_data (out);
     153           1 :   gpgme_data_release (out);
     154             : 
     155           1 :   gpgme_data_release (in);
     156           1 :   gpgme_release (ctx);
     157           1 :   return 0;
     158             : }

Generated by: LCOV version 1.13