LCOV - code coverage report
Current view: top level - tests/gpgsm - t-verify.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 68 67.6 %
Date: 2016-09-12 12:35:26 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* t-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, 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             : static int got_errors;
      38             : 
      39             : static const char test_text1[] = "Hallo Leute!\n";
      40             : static const char test_text1f[]= "Hallo Leute?\n";
      41             : static const char test_sig1[] =
      42             : "-----BEGIN CMS OBJECT-----\n"
      43             : "MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAA\n"
      44             : "MYIBOTCCATUCAQEwcDBrMQswCQYDVQQGEwJERTETMBEGA1UEBxQKRPxzc2VsZG9y\n"
      45             : "ZjEWMBQGA1UEChMNZzEwIENvZGUgR21iSDEZMBcGA1UECxMQQWVneXB0ZW4gUHJv\n"
      46             : "amVjdDEUMBIGA1UEAxMLdGVzdCBjZXJ0IDECAQAwBwYFKw4DAhqgJTAjBgkqhkiG\n"
      47             : "9w0BCQQxFgQU7FC/ibH3lC9GE24RJJxa8zqP7wEwCwYJKoZIhvcNAQEBBIGAA3oC\n"
      48             : "DUmKERmD1eoJYFw38y/qnncS/6ZPjWINDIphZeK8mzAANpvpIaRPf3sNBznb89QF\n"
      49             : "mRgCXIWcjlHT0DTRLBf192Ve22IyKH00L52CqFsSN3a2sajqRUlXH8RY2D+Al71e\n"
      50             : "MYdRclgjObCcoilA8fZ13VR4DiMJVFCxJL4qVWI=\n"
      51             : "-----END CMS OBJECT-----\n";
      52             : 
      53             : 
      54             : static void
      55           2 : check_result (gpgme_verify_result_t result, int summary, char *fpr,
      56             :               gpgme_error_t status, gpgme_validity_t validity)
      57             : {
      58             :   gpgme_signature_t sig;
      59             : 
      60           2 :   sig = result->signatures;
      61           2 :   if (!sig || sig->next)
      62             :     {
      63           0 :       fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
      64             :                __FILE__, __LINE__);
      65           0 :       got_errors = 1;
      66           0 :       if (!sig)
      67           2 :         return;
      68             :     }
      69           2 :   if (sig->summary != summary)
      70             :     {
      71           0 :       fprintf (stderr, "%s:%i: Unexpected signature summary: "
      72             :                "want=0x%x have=0x%x\n",
      73           0 :                __FILE__, __LINE__, summary, sig->summary);
      74           0 :       got_errors = 1;
      75             :     }
      76           2 :   if (sig->fpr && strcmp (sig->fpr, fpr))
      77             :     {
      78           0 :       fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
      79             :                __FILE__, __LINE__, sig->fpr);
      80           0 :       got_errors = 1;
      81             :     }
      82           2 :   if (gpgme_err_code (sig->status) != status)
      83             :     {
      84           0 :       fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
      85             :                __FILE__, __LINE__, gpgme_strerror (sig->status));
      86           0 :       got_errors = 1;
      87             :     }
      88           2 :   if (sig->notations)
      89             :     {
      90           0 :       fprintf (stderr, "%s:%i: Unexpected notation data\n",
      91             :                __FILE__, __LINE__);
      92           0 :       got_errors = 1;
      93             :     }
      94           2 :   if (sig->wrong_key_usage)
      95             :     {
      96           0 :       fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
      97             :                __FILE__, __LINE__);
      98           0 :       got_errors = 1;
      99             :     }
     100           2 :   if (sig->validity != validity)
     101             :     {
     102           0 :       fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
     103           0 :                __FILE__, __LINE__, sig->validity);
     104           0 :       got_errors = 1;
     105             :     }
     106           2 :   if (gpgme_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR)
     107             :     {
     108           0 :       fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
     109             :                __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
     110           0 :       got_errors = 1;
     111             :     }
     112             : }
     113             : 
     114             : 
     115             : static void
     116           2 : show_auditlog (gpgme_ctx_t ctx)
     117             : {
     118             :   gpgme_error_t err;
     119             :   gpgme_data_t data;
     120             : 
     121           2 :   err = gpgme_data_new (&data);
     122           2 :   fail_if_err (err);
     123           2 :   err = gpgme_op_getauditlog (ctx, data, 0);
     124           2 :   if (err)
     125             :     {
     126           0 :       fprintf (stderr, "%s:%i: Can't get audit log: %s\n",
     127             :                __FILE__, __LINE__, gpgme_strerror (err));
     128           0 :       if (gpgme_err_code (err) != GPG_ERR_ASS_UNKNOWN_CMD)
     129           0 :         got_errors = 1;
     130             :     }
     131           2 :   print_data (data);
     132           2 :   gpgme_data_release (data);
     133           2 : }
     134             : 
     135             : 
     136             : 
     137             : int 
     138           1 : main (int argc, char **argv)
     139             : {
     140             :   gpgme_ctx_t ctx;
     141             :   gpgme_error_t err;
     142             :   gpgme_data_t sig, text;
     143             :   gpgme_verify_result_t result;
     144             : 
     145           1 :   init_gpgme (GPGME_PROTOCOL_CMS);
     146             : 
     147           1 :   err = gpgme_new (&ctx);
     148           1 :   fail_if_err (err);
     149           1 :   gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
     150             :   
     151             :   /* Checking a valid message.  */
     152           1 :   err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0);
     153           1 :   fail_if_err (err);
     154           1 :   err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0);
     155           1 :   fail_if_err (err);
     156             : 
     157           1 :   err = gpgme_op_verify (ctx, sig, text, NULL);
     158           1 :   fail_if_err (err);
     159           1 :   result = gpgme_op_verify_result (ctx);
     160           1 :   check_result (result, GPGME_SIGSUM_VALID | GPGME_SIGSUM_GREEN,
     161             :                 "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
     162             :                 GPG_ERR_NO_ERROR, GPGME_VALIDITY_FULL);
     163             : 
     164           1 :   show_auditlog (ctx);
     165             : 
     166             :   /* Checking a manipulated message.  */
     167           1 :   gpgme_data_release (text);
     168           1 :   err = gpgme_data_new_from_mem (&text, test_text1f, strlen (test_text1f), 0);
     169           1 :   fail_if_err (err);
     170           1 :   gpgme_data_seek (sig, 0, SEEK_SET);
     171           1 :   err = gpgme_op_verify (ctx, sig, text, NULL);
     172           1 :   fail_if_err (err);
     173           1 :   result = gpgme_op_verify_result (ctx);
     174           1 :   check_result (result, GPGME_SIGSUM_RED,
     175             :                 "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
     176             :                 GPG_ERR_BAD_SIGNATURE, GPGME_VALIDITY_UNKNOWN);
     177             : 
     178           1 :   show_auditlog (ctx);
     179             : 
     180           1 :   gpgme_data_release (text);
     181           1 :   gpgme_data_release (sig);
     182           1 :   gpgme_release (ctx);  
     183           1 :   return got_errors? 1 : 0;
     184             : }

Generated by: LCOV version 1.11