tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / test_os_start_process.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
21  * @file util/test_os_start_process.c
22  * @brief testcase for os start process code
23  *
24  * This testcase simply calls the os start process code
25  * giving a file descriptor to write stdout to.  If the
26  * correct data "HELLO" is read then all is well.
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "disk.h"
31
32
33 static const char *test_phrase = "HELLO WORLD";
34
35 static int ok;
36
37 static struct GNUNET_OS_Process *proc;
38
39 /**
40  * Pipe to write to started processes stdin (on write end)
41  */
42 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
43
44 /**
45  * Pipe to read from started processes stdout (on read end)
46  */
47 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
48
49 static struct GNUNET_SCHEDULER_Task * die_task;
50
51 struct read_context
52 {
53   char buf[16];
54   int buf_offset;
55   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
56 };
57
58
59 static struct read_context rc;
60
61
62 static void
63 end_task (void *cls)
64 {
65   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
66   {
67     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
68   }
69   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
70   GNUNET_OS_process_destroy (proc);
71   proc = NULL;
72   GNUNET_DISK_pipe_close (hello_pipe_stdout);
73   GNUNET_DISK_pipe_close (hello_pipe_stdin);
74 }
75
76
77 static void
78 read_call (void *cls)
79 {
80   int bytes;
81
82   bytes = GNUNET_DISK_file_read (rc.stdout_read_handle,
83                                  &rc.buf[rc.buf_offset],
84                                  sizeof (rc.buf) - rc.buf_offset);
85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86               "bytes is %d\n",
87               bytes);
88
89   if (bytes < 1)
90   {
91     GNUNET_break (0);
92     ok = 1;
93     GNUNET_SCHEDULER_cancel (die_task);
94     (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
95     return;
96   }
97
98   ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100               "read %s\n",
101               &rc.buf[rc.buf_offset]);
102   rc.buf_offset += bytes;
103
104   if (0 == ok)
105   {
106     GNUNET_SCHEDULER_cancel (die_task);
107     (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
108     return;
109   }
110
111   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
112                                   rc.stdout_read_handle,
113                                   &read_call,
114                                   NULL);
115 }
116
117
118 static void
119 run_task (void *cls)
120 {
121   char *fn;
122   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
123   const struct GNUNET_DISK_FileHandle *wh;
124
125 #if !WINDOWS
126   GNUNET_asprintf (&fn, "cat");
127 #else
128   GNUNET_asprintf (&fn, "w32cat");
129 #endif
130
131   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
132   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
133
134   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
135   {
136     GNUNET_break (0);
137     ok = 1;
138     GNUNET_free (fn);
139     return;
140   }
141
142   proc =
143       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
144                                hello_pipe_stdin, hello_pipe_stdout, NULL,
145                                fn,
146                                "test_gnunet_echo_hello", "-", NULL);
147   GNUNET_free (fn);
148
149   /* Close the write end of the read pipe */
150   GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
151   /* Close the read end of the write pipe */
152   GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
153
154   wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
155
156   /* Write the test_phrase to the cat process */
157   if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
158       strlen (test_phrase) + 1)
159   {
160     GNUNET_break (0);
161     ok = 1;
162     return;
163   }
164
165   /* Close the write end to end the cycle! */
166   GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
167
168   stdout_read_handle =
169       GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
170
171   die_task =
172       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
173                                     (GNUNET_TIME_UNIT_MINUTES, 1),
174                                     &end_task,
175                                     NULL);
176
177   memset (&rc, 0, sizeof (rc));
178   rc.stdout_read_handle = stdout_read_handle;
179   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
180                                   stdout_read_handle,
181                                   &read_call,
182                                   NULL);
183 }
184
185
186 /**
187  * Main method, starts scheduler with task1,
188  * checks that "ok" is correct at the end.
189  */
190 static int
191 check_run ()
192 {
193   ok = 1;
194   GNUNET_SCHEDULER_run (&run_task, &ok);
195   return ok;
196 }
197
198
199 /**
200  * Test killing via pipe.
201  */
202 static int
203 check_kill ()
204 {
205   char *fn;
206
207   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
208   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
209   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
210   {
211     return 1;
212   }
213   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
214   proc =
215     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
216                              hello_pipe_stdin,
217                              hello_pipe_stdout,
218                              NULL,
219                              fn,
220                              "gnunet-service-resolver", "-",
221                              NULL);
222   if (NULL == proc)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225                 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
226     return 77;
227   }
228   sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
229   GNUNET_free (fn);
230   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
231     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
232   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
233   GNUNET_OS_process_destroy (proc);
234   proc = NULL;
235   GNUNET_DISK_pipe_close (hello_pipe_stdout);
236   GNUNET_DISK_pipe_close (hello_pipe_stdin);
237   return 0;
238 }
239
240
241 /**
242  * Test killing via pipe.
243  */
244 static int
245 check_instant_kill ()
246 {
247   char *fn;
248
249   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
250   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
251   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
252   {
253     return 1;
254   }
255   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
256   proc =
257     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
258                              hello_pipe_stdin, hello_pipe_stdout, NULL,
259                              fn,
260                              "gnunet-service-resolver", "-", NULL);
261   if (NULL == proc)
262   {
263     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264                 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
265     return 77;
266   }
267   if (0 != GNUNET_OS_process_kill (proc,
268                                    GNUNET_TERM_SIG))
269   {
270     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
271   }
272   GNUNET_free (fn);
273   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
274   GNUNET_OS_process_destroy (proc);
275   proc = NULL;
276   GNUNET_DISK_pipe_close (hello_pipe_stdout);
277   GNUNET_DISK_pipe_close (hello_pipe_stdin);
278   return 0;
279 }
280
281
282 int
283 main (int argc, char *argv[])
284 {
285   int ret;
286
287   GNUNET_log_setup ("test-os-start-process",
288                     "WARNING",
289                     NULL);
290   ret = 0;
291   ret |= check_run ();
292   ret |= check_kill ();
293   ret |= check_instant_kill ();
294   return ret;
295 }
296
297 /* end of test_os_start_process.c */