1 /* t-eventloop.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, see <https://gnu.org/licenses/>.
19  * SPDX-License-Identifier: LGPL-2.1-or-later
20  */
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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/select.h>
35 
36 #include <gpgme.h>
37 
38 #include "t-support.h"
39 
40 
41 /* Stripped down version of gpgme/wait.c.  */
42 
43 struct op_result
44 {
45   int done;
46   gpgme_error_t err;
47 };
48 
49 struct op_result op_result;
50 
51 struct one_fd
52 {
53   int fd;
54   int dir;
55   gpgme_io_cb_t fnc;
56   void *fnc_data;
57 };
58 
59 #define FDLIST_MAX 32
60 struct one_fd fdlist[FDLIST_MAX];
61 
62 gpgme_error_t
add_io_cb(void * data,int fd,int dir,gpgme_io_cb_t fnc,void * fnc_data,void ** r_tag)63 add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data,
64 	   void **r_tag)
65 {
66   struct one_fd *fds = data;
67   int i;
68 
69   for (i = 0; i < FDLIST_MAX; i++)
70     {
71       if (fds[i].fd == -1)
72 	{
73 	  fds[i].fd = fd;
74 	  fds[i].dir = dir;
75 	  fds[i].fnc = fnc;
76 	  fds[i].fnc_data = fnc_data;
77 	  break;
78 	}
79     }
80   if (i == FDLIST_MAX)
81     return gpgme_err_make (GPG_ERR_SOURCE_USER_1, GPG_ERR_GENERAL);
82   *r_tag = &fds[i];
83   return 0;
84 }
85 
86 void
remove_io_cb(void * tag)87 remove_io_cb (void *tag)
88 {
89   struct one_fd *fd = tag;
90 
91   fd->fd = -1;
92 }
93 
94 void
io_event(void * data,gpgme_event_io_t type,void * type_data)95 io_event (void *data, gpgme_event_io_t type, void *type_data)
96 {
97   struct op_result *result = data;
98 
99   if (type == GPGME_EVENT_DONE)
100     {
101       result->done = 1;
102       result->err = * (gpgme_error_t *) type_data;
103     }
104 }
105 
106 
107 int
do_select(void)108 do_select (void)
109 {
110   fd_set rfds;
111   fd_set wfds;
112   int i, n;
113   int any = 0;
114   struct timeval tv;
115 
116   FD_ZERO (&rfds);
117   FD_ZERO (&wfds);
118   for (i = 0; i < FDLIST_MAX; i++)
119     if (fdlist[i].fd != -1)
120       FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds);
121 
122   tv.tv_sec = 0;
123   tv.tv_usec = 1000;
124 
125   do
126     {
127       n = select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
128     }
129   while (n < 0 && errno == EINTR);
130 
131   if (n < 0)
132     return n;	/* Error or timeout.  */
133 
134   for (i = 0; i < FDLIST_MAX && n; i++)
135     {
136       if (fdlist[i].fd != -1)
137 	{
138 	  if (FD_ISSET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds))
139 	    {
140 	      assert (n);
141 	      n--;
142 	      any = 1;
143 	      (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd);
144 	    }
145 	}
146     }
147   return any;
148 }
149 
150 int
my_wait(void)151 my_wait (void)
152 {
153   int n;
154 
155   do
156     {
157       n = do_select ();
158     }
159   while (n >= 0 && !op_result.done);
160   return 0;
161 }
162 
163 
164 struct gpgme_io_cbs io_cbs =
165   {
166     add_io_cb,
167     fdlist,
168     remove_io_cb,
169     io_event,
170     &op_result
171   };
172 
173 
174 int
main(void)175 main (void)
176 {
177   gpgme_ctx_t ctx;
178   gpgme_error_t err;
179   gpgme_data_t in, out;
180   gpgme_key_t key[3] = { NULL, NULL, NULL };
181   int i;
182 
183   init_gpgme (GPGME_PROTOCOL_OpenPGP);
184 
185   for (i = 0; i < FDLIST_MAX; i++)
186     fdlist[i].fd = -1;
187 
188   err = gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP);
189   fail_if_err (err);
190 
191   err = gpgme_new (&ctx);
192   fail_if_err (err);
193   gpgme_set_armor (ctx, 1);
194   gpgme_set_io_cbs (ctx, &io_cbs);
195   op_result.done = 0;
196 
197   err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
198   fail_if_err (err);
199 
200   err = gpgme_data_new (&out);
201   fail_if_err (err);
202 
203   err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
204 		       &key[0], 0);
205   fail_if_err (err);
206   err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
207 		       &key[1], 0);
208   fail_if_err (err);
209 
210   err = gpgme_op_encrypt_start (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
211   fail_if_err (err);
212 
213   my_wait ();
214   fail_if_err (op_result.err);
215   fail_if_err (err);
216 
217   fflush (NULL);
218   fputs ("Begin Result:\n", stdout);
219   print_data (out);
220   fputs ("End Result.\n", stdout);
221 
222   gpgme_key_unref (key[0]);
223   gpgme_key_unref (key[1]);
224   gpgme_data_release (in);
225   gpgme_data_release (out);
226   gpgme_release (ctx);
227 
228   return 0;
229 }
230