LCOV - code coverage report
Current view: top level - tests/gpg - t-thread1.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 54 59 91.5 %
Date: 2015-11-05 17:14:26 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* t-thread1.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 <errno.h>
      32             : #include <unistd.h>
      33             : #include <pthread.h>
      34             : 
      35             : #include <gpgme.h>
      36             : 
      37             : #include "t-support.h"
      38             : 
      39             : #define ROUNDS 20
      40             : 
      41             : 
      42             : void *
      43           1 : thread_one (void *name)
      44             : {
      45             :   int i;
      46             : 
      47          21 :   for (i = 0; i < ROUNDS; i++)
      48             :     {
      49             :       gpgme_ctx_t ctx;
      50             :       gpgme_error_t err;
      51             :       gpgme_data_t in, out;
      52          20 :       gpgme_key_t key[3] = { NULL, NULL, NULL };
      53             :       gpgme_encrypt_result_t result;
      54             : 
      55          20 :       err = gpgme_new (&ctx);
      56          20 :       fail_if_err (err);
      57          20 :       gpgme_set_armor (ctx, 1);
      58             : 
      59          20 :       err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
      60          20 :       fail_if_err (err);
      61             : 
      62          20 :       err = gpgme_data_new (&out);
      63          20 :       fail_if_err (err);
      64             : 
      65          20 :       err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
      66             :                            &key[0], 0);
      67          20 :       fail_if_err (err);
      68          20 :       err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
      69             :                            &key[1], 0);
      70          20 :       fail_if_err (err);
      71             :       
      72          20 :       err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
      73          20 :       fail_if_err (err);
      74          20 :       result = gpgme_op_encrypt_result (ctx);
      75          20 :       if (result->invalid_recipients)
      76             :         {
      77           0 :           fprintf (stderr, "Invalid recipient encountered: %s\n",
      78           0 :                    result->invalid_recipients->fpr);
      79           0 :           exit (1);
      80             :         }
      81          20 :       printf ("Encrypt %s %i\n", (char *) name, i);
      82             : 
      83          20 :       gpgme_key_unref (key[0]);
      84          20 :       gpgme_key_unref (key[1]);
      85          20 :       gpgme_data_release (in);
      86          20 :       gpgme_data_release (out);
      87          20 :       gpgme_release (ctx);
      88             :     }
      89           1 :   return NULL;
      90             : }
      91             : 
      92             : 
      93             : void *
      94           1 : thread_two (void *name)
      95             : {
      96             :   int i;
      97           1 :   const char *cipher_1_asc = make_filename ("cipher-1.asc");
      98             :   char *agent_info;
      99             : 
     100           1 :   agent_info = getenv("GPG_AGENT_INFO");
     101             : 
     102          21 :   for (i = 0; i < ROUNDS; i++)
     103             :     {
     104             :       gpgme_ctx_t ctx;
     105             :       gpgme_error_t err;
     106             :       gpgme_data_t in, out;
     107             :       gpgme_decrypt_result_t result;
     108             : 
     109          20 :       init_gpgme (GPGME_PROTOCOL_OpenPGP);
     110             : 
     111          20 :       err = gpgme_new (&ctx);
     112          20 :       fail_if_err (err);
     113             : 
     114          20 :       if (!(agent_info && strchr (agent_info, ':')))
     115          20 :         gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
     116             : 
     117          20 :       err = gpgme_data_new_from_file (&in, cipher_1_asc, 1);
     118          20 :       fail_if_err (err);
     119             : 
     120          20 :       err = gpgme_data_new (&out);
     121          20 :       fail_if_err (err);
     122             :       
     123          20 :       err = gpgme_op_decrypt (ctx, in, out);
     124          20 :       fail_if_err (err);
     125          20 :       result = gpgme_op_decrypt_result (ctx);
     126          20 :       if (result->unsupported_algorithm)
     127             :         {
     128           0 :           fprintf (stderr, "%s:%i: unsupported algorithm: %s\n",
     129             :                    __FILE__, __LINE__, result->unsupported_algorithm);
     130           0 :           exit (1);
     131             :         }
     132          20 :       printf ("Decrypt %s %i\n", (char *) name, i);
     133             :    
     134          20 :       gpgme_data_release (in);
     135          20 :       gpgme_data_release (out);
     136          20 :       gpgme_release (ctx);
     137             :     }
     138           1 :   return NULL;
     139             : }
     140             : 
     141             : int 
     142           1 : main (int argc, char *argv[])
     143             : {
     144             :   pthread_t tone;
     145             :   pthread_t ttwo;
     146             : 
     147           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     148             : 
     149           1 :   pthread_create (&tone, NULL, thread_one, "A");
     150           1 :   pthread_create (&ttwo, NULL, thread_two, "B");
     151             : 
     152           1 :   pthread_join (tone, NULL);
     153           1 :   pthread_join (ttwo, NULL);
     154             : 
     155           1 :   return 0;
     156             : }

Generated by: LCOV version 1.11