LCOV - code coverage report
Current view: top level - tests/gpg - t-thread-keylist-verify.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 41 85.4 %
Date: 2016-12-01 18:45:36 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* t-thread-verify.c - Regression test.
       2             :    Copyright (C) 2015 Intevation 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             : #ifdef HAVE_CONFIG_H
      22             : #include <config.h>
      23             : #endif
      24             : 
      25             : #include <stdlib.h>
      26             : #include <stdio.h>
      27             : #include <string.h>
      28             : 
      29             : #include <gpgme.h>
      30             : 
      31             : #include <pthread.h>
      32             : 
      33             : #include "t-support.h"
      34             : 
      35             : #define THREAD_COUNT 100
      36             : 
      37             : static const char test_text1[] = "Just GNU it!\n";
      38             : static const char test_sig1[] =
      39             : "-----BEGIN PGP SIGNATURE-----\n"
      40             : "\n"
      41             : "iN0EABECAJ0FAjoS+i9FFIAAAAAAAwA5YmFyw7bDpMO8w58gZGFzIHdhcmVuIFVt\n"
      42             : "bGF1dGUgdW5kIGpldHp0IGVpbiBwcm96ZW50JS1aZWljaGVuNRSAAAAAAAgAJGZv\n"
      43             : "b2Jhci4xdGhpcyBpcyBhIG5vdGF0aW9uIGRhdGEgd2l0aCAyIGxpbmVzGhpodHRw\n"
      44             : "Oi8vd3d3Lmd1Lm9yZy9wb2xpY3kvAAoJEC1yfMdoaXc0JBIAoIiLlUsvpMDOyGEc\n"
      45             : "dADGKXF/Hcb+AKCJWPphZCphduxSvrzH0hgzHdeQaA==\n"
      46             : "=nts1\n"
      47             : "-----END PGP SIGNATURE-----\n";
      48             : 
      49             : void *
      50          99 : start_keylist (void *arg)
      51             : {
      52             :   gpgme_error_t err;
      53             :   gpgme_ctx_t ctx;
      54             :   gpgme_key_t key;
      55             : 
      56             :   (void)arg;
      57          99 :   err = gpgme_new (&ctx);
      58         100 :   fail_if_err (err);
      59             : 
      60         100 :   err = gpgme_op_keylist_start (ctx, NULL, 0);
      61         100 :   fail_if_err (err);
      62             : 
      63        2900 :   while (!(err = gpgme_op_keylist_next (ctx, &key)))
      64             :     {
      65        2698 :       gpgme_key_unref (key);
      66             :     }
      67             : 
      68         100 :   gpgme_release (ctx);
      69         100 :   return NULL;
      70             : }
      71             : 
      72             : void *
      73          99 : start_verify (void *arg)
      74             : {
      75             :   gpgme_ctx_t ctx;
      76             :   gpgme_error_t err;
      77             :   gpgme_data_t sig, text;
      78             :   gpgme_verify_result_t result;
      79             :   gpgme_signature_t signature;
      80             : 
      81             :   (void)arg;
      82             : 
      83          99 :   err = gpgme_new (&ctx);
      84         100 :   fail_if_err (err);
      85             : 
      86             :   /* Checking a valid message.  */
      87         100 :   err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0);
      88         100 :   fail_if_err (err);
      89         100 :   err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0);
      90         100 :   fail_if_err (err);
      91         100 :   err = gpgme_op_verify (ctx, sig, text, NULL);
      92         100 :   fail_if_err (err);
      93         100 :   result = gpgme_op_verify_result (ctx);
      94             : 
      95         100 :   signature = result->signatures;
      96             : 
      97         100 :   if (strcmp (signature->fpr, "A0FF4590BB6122EDEF6E3C542D727CC768697734"))
      98             :     {
      99           0 :       fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
     100             :                __FILE__, __LINE__, signature->fpr);
     101           0 :       exit (1);
     102             :     }
     103         100 :   if (gpgme_err_code (signature->status) != GPG_ERR_NO_ERROR)
     104             :     {
     105           0 :       fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
     106             :                __FILE__, __LINE__, gpgme_strerror (signature->status));
     107           0 :       exit (1);
     108             :     }
     109         100 :   gpgme_free (text);
     110         100 :   gpgme_free (sig);
     111         100 :   gpgme_release (ctx);
     112         100 :   return NULL;
     113             : }
     114             : 
     115             : int
     116           1 : main (int argc, char *argv[])
     117             : {
     118             :   int i;
     119             :   pthread_t verify_threads[THREAD_COUNT];
     120             :   pthread_t keylist_threads[THREAD_COUNT];
     121           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     122             : 
     123             :   (void)argc;
     124             :   (void)argv;
     125             : 
     126         101 :   for (i = 0; i < THREAD_COUNT; i++)
     127             :     {
     128         200 :       if (pthread_create(&verify_threads[i], NULL, start_verify, NULL) ||
     129         100 :           pthread_create(&keylist_threads[i], NULL, start_keylist, NULL))
     130             :         {
     131           0 :           fprintf(stderr, "%s:%i: failed to create threads \n",
     132             :                        __FILE__, __LINE__);
     133           0 :           exit(1);
     134             :         }
     135             :    }
     136         101 :   for (i = 0; i < THREAD_COUNT; i++)
     137             :     {
     138         100 :       pthread_join (verify_threads[i], NULL);
     139         100 :       pthread_join (keylist_threads[i], NULL);
     140             :     }
     141           1 :   return 0;
     142             : }

Generated by: LCOV version 1.11