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