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_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 GNUNET_SCHEDULER_TaskIdentifier die_task;
55 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
58 struct read_context rc;
61 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
65 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
67 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
68 GNUNET_OS_process_destroy (proc);
70 GNUNET_DISK_pipe_close (hello_pipe_stdout);
71 GNUNET_DISK_pipe_close (hello_pipe_stdin);
76 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 bytes = GNUNET_DISK_file_read (rc.stdout_read_handle, &rc.buf[rc.buf_offset], \
81 sizeof (rc.buf) - rc.buf_offset);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "bytes is %d\n", bytes);
89 GNUNET_SCHEDULER_cancel (die_task);
90 GNUNET_SCHEDULER_add_now (&end_task, NULL);
94 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "read %s\n", &rc.buf[rc.buf_offset]);
96 rc.buf_offset += bytes;
100 GNUNET_SCHEDULER_cancel (die_task);
101 GNUNET_SCHEDULER_add_now (&end_task, NULL);
105 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
106 rc.stdout_read_handle, &read_call,
113 run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
117 const struct GNUNET_DISK_FileHandle *wh;
120 GNUNET_asprintf (&fn, "cat");
122 GNUNET_asprintf (&fn, "w32cat");
125 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
126 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
128 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
137 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
138 hello_pipe_stdin, hello_pipe_stdout, fn,
139 "test_gnunet_echo_hello", "-", NULL);
142 /* Close the write end of the read pipe */
143 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
144 /* Close the read end of the write pipe */
145 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
147 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
149 /* Write the test_phrase to the cat process */
150 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
151 strlen (test_phrase) + 1)
158 /* Close the write end to end the cycle! */
159 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
162 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
165 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
166 (GNUNET_TIME_UNIT_MINUTES, 1), &end_task,
169 memset (&rc, 0, sizeof (rc));
170 rc.stdout_read_handle = stdout_read_handle;
171 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
172 stdout_read_handle, &read_call,
178 * Main method, starts scheduler with task1,
179 * checks that "ok" is correct at the end.
185 GNUNET_SCHEDULER_run (&run_task, &ok);
191 * Test killing via pipe.
198 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
199 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
200 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
204 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
206 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
207 "gnunet-service-resolver", "-", NULL);
208 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
210 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
211 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
212 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
213 GNUNET_OS_process_destroy (proc);
215 GNUNET_DISK_pipe_close (hello_pipe_stdout);
216 GNUNET_DISK_pipe_close (hello_pipe_stdin);
222 * Test killing via pipe.
225 check_instant_kill ()
229 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
230 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
231 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
235 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
237 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn,
238 "gnunet-service-resolver", "-", NULL);
239 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
241 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 */