LCOV - code coverage report
Current view: top level - tests/gpg - t-decrypt-verify.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 55 63.6 %
Date: 2016-11-29 15:07:43 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* t-decrypt-verify.c - Regression test.
       2             :  * Copyright (C) 2000 Werner Koch (dd9jn)
       3             :  * Copyright (C) 2001, 2002, 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, see <https://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : /* We need to include config.h so that we know whether we are building
      22             :    with large file system (LFS) support. */
      23             : #ifdef HAVE_CONFIG_H
      24             : #include <config.h>
      25             : #endif
      26             : 
      27             : #include <stdlib.h>
      28             : #include <stdio.h>
      29             : #include <string.h>
      30             : #include <errno.h>
      31             : #include <unistd.h>
      32             : 
      33             : #include <gpgme.h>
      34             : 
      35             : #include "t-support.h"
      36             : 
      37             : 
      38             : static void
      39           1 : check_verify_result (gpgme_verify_result_t result, unsigned int summary,
      40             :                      const char *fpr, gpgme_error_t status)
      41             : {
      42             :   gpgme_signature_t sig;
      43             : 
      44           1 :   sig = result->signatures;
      45           1 :   if (!sig || sig->next)
      46             :     {
      47           0 :       fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
      48             :                __FILE__, __LINE__);
      49           0 :       exit (1);
      50             :     }
      51           1 :   if (sig->summary != summary)
      52             :     {
      53           0 :       fprintf (stderr, "%s:%i: Unexpected signature summary: 0x%x\n",
      54           0 :                __FILE__, __LINE__, sig->summary);
      55           0 :       exit (1);
      56             :     }
      57           1 :   if (strcmp (sig->fpr, fpr))
      58             :     {
      59           0 :       fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
      60             :                __FILE__, __LINE__, sig->fpr);
      61           0 :       exit (1);
      62             :     }
      63           1 :   if (gpgme_err_code (sig->status) != status)
      64             :     {
      65           0 :       fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
      66             :                __FILE__, __LINE__, gpgme_strerror (sig->status));
      67           0 :       exit (1);
      68             :     }
      69           1 :   if (sig->notations)
      70             :     {
      71           0 :       fprintf (stderr, "%s:%i: Unexpected notation data\n",
      72             :                __FILE__, __LINE__);
      73           0 :       exit (1);
      74             :     }
      75           1 :   if (sig->wrong_key_usage)
      76             :     {
      77           0 :       fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
      78             :                __FILE__, __LINE__);
      79           0 :       exit (1);
      80             :     }
      81           1 :   if (sig->validity != GPGME_VALIDITY_UNKNOWN)
      82             :     {
      83           0 :       fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
      84           0 :                __FILE__, __LINE__, sig->validity);
      85           0 :       exit (1);
      86             :     }
      87           1 :   if (gpgme_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR)
      88             :     {
      89           0 :       fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
      90             :                __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
      91           0 :       exit (1);
      92             :     }
      93           1 : }
      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_decrypt_result_t decrypt_result;
     103             :   gpgme_verify_result_t verify_result;
     104           1 :   char *cipher_2_asc = make_filename ("cipher-2.asc");
     105             :   char *agent_info;
     106             : 
     107             :   (void)argc;
     108             :   (void)argv;
     109             : 
     110           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     111             : 
     112           1 :   err = gpgme_new (&ctx);
     113           1 :   fail_if_err (err);
     114             : 
     115           1 :   agent_info = getenv("GPG_AGENT_INFO");
     116           1 :   if (!(agent_info && strchr (agent_info, ':')))
     117           1 :     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
     118             : 
     119           1 :   err = gpgme_data_new_from_file (&in, cipher_2_asc, 1);
     120           1 :   free (cipher_2_asc);
     121           1 :   fail_if_err (err);
     122           1 :   err = gpgme_data_new (&out);
     123           1 :   fail_if_err (err);
     124             : 
     125           1 :   err = gpgme_op_decrypt_verify (ctx, in, out);
     126           1 :   fail_if_err (err);
     127           1 :   decrypt_result = gpgme_op_decrypt_result (ctx);
     128           1 :   if (decrypt_result->unsupported_algorithm)
     129             :     {
     130           0 :       fprintf (stderr, "%s:%i: unsupported algorithm: %s\n",
     131             :                __FILE__, __LINE__, decrypt_result->unsupported_algorithm);
     132           0 :       exit (1);
     133             :     }
     134           1 :   print_data (out);
     135           1 :   verify_result = gpgme_op_verify_result (ctx);
     136           1 :   check_verify_result (verify_result, 0,
     137             :                        "A0FF4590BB6122EDEF6E3C542D727CC768697734",
     138             :                        GPG_ERR_NO_ERROR);
     139             : 
     140           1 :   gpgme_data_release (in);
     141           1 :   gpgme_data_release (out);
     142           1 :   gpgme_release (ctx);
     143           1 :   return 0;
     144             : }

Generated by: LCOV version 1.11