2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file util/test_os_start_process.c
22 * @brief testcase for os start process code
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.
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"
37 static const char *test_phrase = "HELLO WORLD";
41 static struct GNUNET_OS_Process *proc;
44 * Pipe to write to started processes stdin (on write end)
46 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
49 * Pipe to read from started processes stdout (on read end)
51 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
53 static GNUNET_SCHEDULER_TaskIdentifier die_task;
57 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
59 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
61 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
63 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
64 GNUNET_OS_process_destroy (proc);
66 GNUNET_DISK_pipe_close (hello_pipe_stdout);
67 GNUNET_DISK_pipe_close (hello_pipe_stdin);
72 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
77 memset (&buf, 0, sizeof (buf));
80 bytes = GNUNET_DISK_file_read (stdout_read_handle, &buf, sizeof (buf));
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "bytes is %d\n", bytes);
88 GNUNET_SCHEDULER_cancel (die_task);
89 GNUNET_SCHEDULER_add_now (&end_task, NULL);
93 ok = strncmp (&buf[0], test_phrase, strlen (test_phrase));
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "read %s\n", &buf[0]);
97 GNUNET_SCHEDULER_cancel (die_task);
98 GNUNET_SCHEDULER_add_now (&end_task, NULL);
102 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
103 stdout_read_handle, &read_call,
110 run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
114 const struct GNUNET_DISK_FileHandle *wh;
117 GNUNET_asprintf (&fn, "cat");
119 GNUNET_asprintf (&fn, "w32cat");
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);
125 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
134 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
135 "test_gnunet_echo_hello", "-", NULL);
138 /* Close the write end of the read pipe */
139 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
140 /* Close the read end of the write pipe */
141 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
143 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
145 /* Write the test_phrase to the cat process */
146 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
147 strlen (test_phrase) + 1)
154 /* Close the write end to end the cycle! */
155 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
158 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
161 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
162 (GNUNET_TIME_UNIT_MINUTES, 1), &end_task,
165 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
166 stdout_read_handle, &read_call,
167 (void *) stdout_read_handle);
172 * Main method, starts scheduler with task1,
173 * checks that "ok" is correct at the end.
179 GNUNET_SCHEDULER_run (&run_task, &ok);
185 * Test killing via pipe.
192 GNUNET_asprintf (&fn, "cat");
194 GNUNET_asprintf (&fn, "w32cat");
196 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
197 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
198 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
204 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
205 "gnunet-service-resolver", "-", NULL);
206 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
207 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
208 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
209 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
210 GNUNET_OS_process_destroy (proc);
212 GNUNET_DISK_pipe_close (hello_pipe_stdout);
213 GNUNET_DISK_pipe_close (hello_pipe_stdin);
219 * Test killing via pipe.
222 check_instant_kill ()
226 GNUNET_asprintf (&fn, "cat");
228 GNUNET_asprintf (&fn, "w32cat");
230 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
231 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
232 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
238 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
239 "gnunet-service-resolver", "-", NULL);
240 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
242 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
244 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
245 GNUNET_OS_process_destroy (proc);
247 GNUNET_DISK_pipe_close (hello_pipe_stdout);
248 GNUNET_DISK_pipe_close (hello_pipe_stdin);
254 main (int argc, char *argv[])
258 GNUNET_log_setup ("test-os-start-process",
263 ret |= check_kill ();
264 ret |= check_instant_kill ();
268 /* end of test_os_start_process.c */