LCOV - code coverage report
Current view: top level - tests/gpg - t-sig-notation.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 57 63 90.5 %
Date: 2015-11-05 17:14:26 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* t-sig-notation.c - Regression test.
       2             :    Copyright (C) 2005 g10 Code GmbH
       3             : 
       4             :    This file is part of GPGME.
       5             :  
       6             :    GPGME is free software; you can redistribute it and/or modify it
       7             :    under the terms of the GNU Lesser General Public License as
       8             :    published by the Free Software Foundation; either version 2.1 of
       9             :    the License, or (at your option) any later version.
      10             :    
      11             :    GPGME is distributed in the hope that it will be useful, but
      12             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :    Lesser General Public License for more details.
      15             :    
      16             :    You should have received a copy of the GNU Lesser General Public
      17             :    License along with this program; if not, write to the Free Software
      18             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      19             :    02111-1307, USA.  */
      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             : 
      31             : #include <gpgme.h>
      32             : 
      33             : #include "t-support.h"
      34             : 
      35             : 
      36             : static struct {
      37             :   const char *name;
      38             :   const char *value;
      39             :   gpgme_sig_notation_flags_t flags;
      40             :   int seen;
      41             : } expected_notations[] = { 
      42             :   { "laughing@me",
      43             :     "Just Squeeze Me",
      44             :     GPGME_SIG_NOTATION_HUMAN_READABLE },
      45             :   { "preferred-email-encoding@pgp.com",
      46             :     "pgpmime",
      47             :     GPGME_SIG_NOTATION_HUMAN_READABLE | GPGME_SIG_NOTATION_CRITICAL },
      48             :   { NULL, 
      49             :     "http://www.gnu.org/policy/",
      50             :     0 }
      51             : };
      52             : 
      53             : static void
      54           1 : check_result (gpgme_verify_result_t result)
      55             : {
      56             :   int i;
      57             :   gpgme_sig_notation_t r;
      58             :   
      59             :   gpgme_signature_t sig;
      60             : 
      61           1 :   sig = result->signatures;
      62           1 :   if (!sig || sig->next)
      63             :     {
      64           0 :       fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
      65             :                __FILE__, __LINE__);
      66           0 :       exit (1);
      67             :     }
      68             : 
      69           4 :   for (i=0; i < DIM(expected_notations); i++ )
      70           3 :     expected_notations[i].seen = 0;
      71             :   
      72           4 :   for (r = result->signatures->notations; r; r = r->next)
      73             :     {
      74           3 :       int any = 0;
      75          12 :       for (i=0; i < DIM(expected_notations); i++)
      76             :         {
      77           9 :           if ( ((r->name && expected_notations[i].name
      78           4 :                  && !strcmp (r->name, expected_notations[i].name)
      79           4 :                  && r->name_len
      80           2 :                  == strlen (expected_notations[i].name))
      81           7 :                 || (!r->name && !expected_notations[i].name
      82           1 :                     && r->name_len == 0))
      83           3 :                && r->value
      84           3 :                && !strcmp (r->value, expected_notations[i].value)
      85           3 :                && r->value_len == strlen (expected_notations[i].value)
      86           6 :                && r->flags
      87           3 :                == (expected_notations[i].flags & ~GPGME_SIG_NOTATION_CRITICAL)
      88           6 :                && r->human_readable
      89           3 :                == !!(r->flags & GPGME_SIG_NOTATION_HUMAN_READABLE)
      90           3 :                && r->critical == 0)
      91             :             {
      92           3 :               expected_notations[i].seen++;
      93           3 :               any++;
      94             :             }
      95             :         }
      96           3 :       if (!any)
      97             :         {
      98           0 :           fprintf (stderr, "%s:%i: Unexpected notation data\n",
      99             :                    __FILE__, __LINE__);
     100           0 :           exit (1);
     101             :         }
     102             :     }
     103           4 :   for (i=0; i < DIM(expected_notations); i++ )
     104             :     {
     105           3 :       if (expected_notations[i].seen != 1)
     106             :         {
     107           0 :           fprintf (stderr, "%s:%i: Missing or duplicate notation data\n",
     108             :                    __FILE__, __LINE__);
     109           0 :           exit (1);
     110             :         }
     111             :     }
     112           1 : }
     113             : 
     114             : 
     115             : int 
     116           1 : main (int argc, char *argv[])
     117             : {
     118             :   gpgme_ctx_t ctx;
     119             :   gpgme_error_t err;
     120             :   gpgme_data_t in, out;
     121             :   gpgme_verify_result_t result;
     122             :   char *agent_info;
     123             :   int i;
     124             : 
     125           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     126             : 
     127           1 :   err = gpgme_new (&ctx);
     128           1 :   fail_if_err (err);
     129             : 
     130           1 :   agent_info = getenv ("GPG_AGENT_INFO");
     131           1 :   if (!(agent_info && strchr (agent_info, ':')))
     132           1 :     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
     133             : 
     134           1 :   err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
     135           1 :   fail_if_err (err);
     136           1 :   err = gpgme_data_new (&out);
     137           1 :   fail_if_err (err);
     138             : 
     139           5 :   for (i = 0; i < sizeof (expected_notations) / sizeof (expected_notations[0]);
     140           3 :        i++)
     141             :     {
     142           3 :       err = gpgme_sig_notation_add (ctx, expected_notations[i].name,
     143             :                                     expected_notations[i].value,
     144             :                                     expected_notations[i].flags);
     145           3 :       fail_if_err (err);
     146             :     }
     147             :   
     148           1 :   err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
     149           1 :   fail_if_err (err);
     150             : 
     151           1 :   gpgme_data_release (in);
     152           1 :   err = gpgme_data_new (&in);
     153           1 :   fail_if_err (err);
     154             : 
     155           1 :   gpgme_data_seek (out, 0, SEEK_SET);
     156             : 
     157           1 :   err = gpgme_op_verify (ctx, out, NULL, in);
     158           1 :   fail_if_err (err);
     159           1 :   result = gpgme_op_verify_result (ctx);
     160           1 :   check_result (result);
     161             : 
     162           1 :   gpgme_data_release (in);
     163           1 :   gpgme_data_release (out);
     164           1 :   gpgme_release (ctx);
     165           1 :   return 0;
     166             : }

Generated by: LCOV version 1.11