-removing 2nd argument from GNUNET_CLIENT_disconnect as it was virtually always GNUNE...
[oweals/gnunet.git] / src / util / test_os_start_process.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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_common.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "disk.h"
35
36 #define VERBOSE GNUNET_NO
37
38 static char *test_phrase = "HELLO WORLD";
39 static int ok;
40
41 static struct GNUNET_OS_Process *proc;
42
43 /* Pipe to write to started processes stdin (on write end) */
44 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
45
46 /* Pipe to read from started processes stdout (on read end) */
47 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
48
49 static GNUNET_SCHEDULER_TaskIdentifier die_task;
50
51 static void
52 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53 {
54   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
55   {
56     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
57   }
58   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
59   GNUNET_OS_process_close (proc);
60   proc = NULL;
61   GNUNET_DISK_pipe_close (hello_pipe_stdout);
62   GNUNET_DISK_pipe_close (hello_pipe_stdin);
63 }
64
65 static void
66 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 {
68   struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
69   char buf[16];
70
71   memset (&buf, 0, sizeof (buf));
72   int bytes;
73
74   bytes = GNUNET_DISK_file_read (stdout_read_handle, &buf, sizeof (buf));
75
76 #if VERBOSE
77   FPRINTF (stderr, "bytes is %d\n", bytes);
78 #endif
79
80   if (bytes < 1)
81   {
82     GNUNET_break (0);
83     ok = 1;
84     GNUNET_SCHEDULER_cancel (die_task);
85     GNUNET_SCHEDULER_add_now (&end_task, NULL);
86     return;
87   }
88
89   ok = strncmp (&buf[0], test_phrase, strlen (test_phrase));
90 #if VERBOSE
91   FPRINTF (stderr, "read %s\n", &buf[0]);
92 #endif
93   if (ok == 0)
94   {
95     GNUNET_SCHEDULER_cancel (die_task);
96     GNUNET_SCHEDULER_add_now (&end_task, NULL);
97     return;
98   }
99
100   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
101                                   stdout_read_handle, &read_call,
102                                   stdout_read_handle);
103
104 }
105
106
107 static void
108 run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   char *fn;
111   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
112   const struct GNUNET_DISK_FileHandle *wh;
113
114   GNUNET_asprintf (&fn, "cat");
115
116   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
117   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
118
119   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
120   {
121     GNUNET_break (0);
122     ok = 1;
123     GNUNET_free (fn);
124     return;
125   }
126
127   proc =
128       GNUNET_OS_start_process (GNUNET_NO, hello_pipe_stdin, hello_pipe_stdout, fn,
129                                "test_gnunet_echo_hello", "-", NULL);
130   GNUNET_free (fn);
131
132   /* Close the write end of the read pipe */
133   GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
134   /* Close the read end of the write pipe */
135   GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
136
137   wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
138
139   /* Write the test_phrase to the cat process */
140   if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
141       strlen (test_phrase) + 1)
142   {
143     GNUNET_break (0);
144     ok = 1;
145     return;
146   }
147
148   /* Close the write end to end the cycle! */
149   GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
150
151   stdout_read_handle =
152       GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
153
154   die_task =
155       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
156                                     (GNUNET_TIME_UNIT_MINUTES, 1), &end_task,
157                                     NULL);
158
159   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
160                                   stdout_read_handle, &read_call,
161                                   (void *) stdout_read_handle);
162 }
163
164
165 /**
166  * Main method, starts scheduler with task1,
167  * checks that "ok" is correct at the end.
168  */
169 static int
170 check_run ()
171 {
172   ok = 1;
173   GNUNET_SCHEDULER_run (&run_task, &ok);
174   return ok;
175 }
176
177
178 /**
179  * Test killing via pipe.
180  */
181 static int
182 check_kill ()
183 {
184   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
185   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
186   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
187   {
188     return 1;
189   }
190   proc =
191     GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, "cat",
192                              "gnunet-service-resolver", "-", NULL); 
193   sleep (1); /* give process time to start and open pipe */
194   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
195   {
196     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
197   }
198   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
199   GNUNET_OS_process_close (proc);
200   proc = NULL;
201   GNUNET_DISK_pipe_close (hello_pipe_stdout);
202   GNUNET_DISK_pipe_close (hello_pipe_stdin);
203   return 0;
204 }
205
206
207 /**
208  * Test killing via pipe.
209  */
210 static int
211 check_instant_kill ()
212 {
213   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
214   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
215   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
216   {
217     return 1;
218   }
219   proc =
220     GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, "cat",
221                              "gnunet-service-resolver", "-", NULL); 
222   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
223   {
224     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
225   }
226   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
227   GNUNET_OS_process_close (proc);
228   proc = NULL;
229   GNUNET_DISK_pipe_close (hello_pipe_stdout);
230   GNUNET_DISK_pipe_close (hello_pipe_stdin);
231   return 0;
232 }
233
234
235 int
236 main (int argc, char *argv[])
237 {
238   int ret;
239
240   GNUNET_log_setup ("test-os-start-process",
241 #if VERBOSE
242                     "DEBUG",
243 #else
244                     "WARNING",
245 #endif
246                     NULL);
247   ret = 0;
248   ret |= check_run ();
249   ret |= check_kill ();
250   ret |= check_instant_kill ();
251
252   return ret;
253 }