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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file util/test_os_start_process.c
20 * @brief testcase for os start process code
22 * This testcase simply calls the os start process code
23 * giving a file descriptor to write stdout to. If the
24 * correct data "HELLO" is read then all is well.
27 #include "gnunet_util_lib.h"
31 static const char *test_phrase = "HELLO WORLD";
35 static struct GNUNET_OS_Process *proc;
38 * Pipe to write to started processes stdin (on write end)
40 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
43 * Pipe to read from started processes stdout (on read end)
45 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
47 static struct GNUNET_SCHEDULER_Task * die_task;
53 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
57 static struct read_context rc;
63 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
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);
80 bytes = GNUNET_DISK_file_read (rc.stdout_read_handle,
81 &rc.buf[rc.buf_offset],
82 sizeof (rc.buf) - rc.buf_offset);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 GNUNET_SCHEDULER_cancel (die_task);
92 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
96 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 &rc.buf[rc.buf_offset]);
100 rc.buf_offset += bytes;
104 GNUNET_SCHEDULER_cancel (die_task);
105 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
109 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
110 rc.stdout_read_handle,
120 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
121 const struct GNUNET_DISK_FileHandle *wh;
124 GNUNET_asprintf (&fn, "cat");
126 GNUNET_asprintf (&fn, "w32cat");
129 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
130 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
132 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
141 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
142 hello_pipe_stdin, hello_pipe_stdout, NULL,
144 "test_gnunet_echo_hello", "-", NULL);
147 /* Close the write end of the read pipe */
148 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
149 /* Close the read end of the write pipe */
150 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
152 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
154 /* Write the test_phrase to the cat process */
155 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
156 strlen (test_phrase) + 1)
163 /* Close the write end to end the cycle! */
164 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
167 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
171 (GNUNET_TIME_UNIT_MINUTES, 1),
175 memset (&rc, 0, sizeof (rc));
176 rc.stdout_read_handle = stdout_read_handle;
177 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
185 * Main method, starts scheduler with task1,
186 * checks that "ok" is correct at the end.
192 GNUNET_SCHEDULER_run (&run_task, &ok);
198 * Test killing via pipe.
205 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
206 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
207 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
211 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
213 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
218 "gnunet-service-resolver", "-",
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
226 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
228 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
229 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
230 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
231 GNUNET_OS_process_destroy (proc);
233 GNUNET_DISK_pipe_close (hello_pipe_stdout);
234 GNUNET_DISK_pipe_close (hello_pipe_stdin);
240 * Test killing via pipe.
243 check_instant_kill ()
247 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
248 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
249 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
253 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
255 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
256 hello_pipe_stdin, hello_pipe_stdout, NULL,
258 "gnunet-service-resolver", "-", NULL);
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 "Failed to launch gnunet-service-resolver. Is your system setup correct?\n");
265 if (0 != GNUNET_OS_process_kill (proc,
268 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
271 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
272 GNUNET_OS_process_destroy (proc);
274 GNUNET_DISK_pipe_close (hello_pipe_stdout);
275 GNUNET_DISK_pipe_close (hello_pipe_stdin);
281 main (int argc, char *argv[])
285 GNUNET_log_setup ("test-os-start-process",
290 ret |= check_kill ();
291 ret |= check_instant_kill ();
295 /* end of test_os_start_process.c */