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.
16 * @file util/test_os_start_process.c
17 * @brief testcase for os start process code
19 * This testcase simply calls the os start process code
20 * giving a file descriptor to write stdout to. If the
21 * correct data "HELLO" is read then all is well.
24 #include "gnunet_util_lib.h"
28 static const char *test_phrase = "HELLO WORLD";
32 static struct GNUNET_OS_Process *proc;
35 * Pipe to write to started processes stdin (on write end)
37 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
40 * Pipe to read from started processes stdout (on read end)
42 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
44 static struct GNUNET_SCHEDULER_Task * die_task;
50 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
54 static struct read_context rc;
60 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
62 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
64 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
65 GNUNET_OS_process_destroy (proc);
67 GNUNET_DISK_pipe_close (hello_pipe_stdout);
68 GNUNET_DISK_pipe_close (hello_pipe_stdin);
77 bytes = GNUNET_DISK_file_read (rc.stdout_read_handle,
78 &rc.buf[rc.buf_offset],
79 sizeof (rc.buf) - rc.buf_offset);
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 GNUNET_SCHEDULER_cancel (die_task);
89 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
93 ok = strncmp (rc.buf, test_phrase, strlen (test_phrase));
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 &rc.buf[rc.buf_offset]);
97 rc.buf_offset += bytes;
101 GNUNET_SCHEDULER_cancel (die_task);
102 (void) GNUNET_SCHEDULER_add_now (&end_task, NULL);
106 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
107 rc.stdout_read_handle,
117 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
118 const struct GNUNET_DISK_FileHandle *wh;
121 GNUNET_asprintf (&fn, "cat");
123 GNUNET_asprintf (&fn, "w32cat");
126 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
127 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
129 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
138 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR,
139 hello_pipe_stdin, hello_pipe_stdout, NULL,
141 "test_gnunet_echo_hello", "-", NULL);
144 /* Close the write end of the read pipe */
145 GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
146 /* Close the read end of the write pipe */
147 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
149 wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
151 /* Write the test_phrase to the cat process */
152 if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) !=
153 strlen (test_phrase) + 1)
160 /* Close the write end to end the cycle! */
161 GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
164 GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
167 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
168 (GNUNET_TIME_UNIT_MINUTES, 1),
172 memset (&rc, 0, sizeof (rc));
173 rc.stdout_read_handle = stdout_read_handle;
174 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
182 * Main method, starts scheduler with task1,
183 * checks that "ok" is correct at the end.
189 GNUNET_SCHEDULER_run (&run_task, &ok);
195 * Test killing via pipe.
202 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
203 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
204 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
208 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
210 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
215 "gnunet-service-resolver", "-",
217 sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
219 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
220 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
221 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
222 GNUNET_OS_process_destroy (proc);
224 GNUNET_DISK_pipe_close (hello_pipe_stdout);
225 GNUNET_DISK_pipe_close (hello_pipe_stdin);
231 * Test killing via pipe.
234 check_instant_kill ()
238 hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
239 hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
240 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
244 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
246 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR,
247 hello_pipe_stdin, hello_pipe_stdout, NULL,
249 "gnunet-service-resolver", "-", NULL);
250 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
252 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
255 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc));
256 GNUNET_OS_process_destroy (proc);
258 GNUNET_DISK_pipe_close (hello_pipe_stdout);
259 GNUNET_DISK_pipe_close (hello_pipe_stdin);
265 main (int argc, char *argv[])
269 GNUNET_log_setup ("test-os-start-process",
274 ret |= check_kill ();
275 ret |= check_instant_kill ();
279 /* end of test_os_start_process.c */