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