2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_util_lib.h"
33 static const char *test_phrase = "HELLO WORLD";
37 static struct GNUNET_OS_Process *proc;
40 * Pipe to write to started processes stdin (on write end)
42 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
45 * Pipe to read from started processes stdout (on read end)
47 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
49 static struct GNUNET_SCHEDULER_Task * die_task;
55 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
59 static struct read_context rc;
65 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
67 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
69 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
70 GNUNET_OS_process_destroy (proc);
72 GNUNET_DISK_pipe_close (hello_pipe_stdout);
73 GNUNET_DISK_pipe_close (hello_pipe_stdin);
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,
93 GNUNET_SCHEDULER_cancel (die_task);
94 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
98 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 &rc.buf[rc.buf_offset]);
102 rc.buf_offset += bytes;
106 GNUNET_SCHEDULER_cancel (die_task);
107 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
111 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
112 rc.stdout_read_handle,
122 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
123 const struct GNUNET_DISK_FileHandle *wh;
126 GNUNET_asprintf (&fn, "cat");
128 GNUNET_asprintf (&fn, "w32cat");
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);
134 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
143 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
144 hello_pipe_stdin, hello_pipe_stdout, NULL,
146 "test_gnunet_echo_hello", "-", NULL);
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);
154 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
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)
165 /* Close the write end to end the cycle! */
166 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
169 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
172 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
173 (GNUNET_TIME_UNIT_MINUTES, 1),
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,
187 * Main method, starts scheduler with task1,
188 * checks that "ok" is correct at the end.
194 GNUNET_SCHEDULER_run (&run_task, &ok);
200 * Test killing via pipe.
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))
213 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
215 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
220 "gnunet-service-resolver", "-",
222 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
224 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
225 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
226 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
227 GNUNET_OS_process_destroy (proc);
229 GNUNET_DISK_pipe_close (hello_pipe_stdout);
230 GNUNET_DISK_pipe_close (hello_pipe_stdin);
236 * Test killing via pipe.
239 check_instant_kill ()
243 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
244 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
245 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
249 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
251 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
252 hello_pipe_stdin, hello_pipe_stdout, NULL,
254 "gnunet-service-resolver", "-", NULL);
255 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
260 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
261 GNUNET_OS_process_destroy (proc);
263 GNUNET_DISK_pipe_close (hello_pipe_stdout);
264 GNUNET_DISK_pipe_close (hello_pipe_stdin);
270 main (int argc, char *argv[])
274 GNUNET_log_setup ("test-os-start-process",
279 ret |= check_kill ();
280 ret |= check_instant_kill ();
284 /* end of test_os_start_process.c */