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