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 2, 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.
28 * TODO: This test case will not work on windows because
29 * there is no cat (unless there is). Perhaps we should
30 * add a gnunet_cat program/test program to util so we can
31 * adequately test this functionality on windows?
34 #include "gnunet_common.h"
35 #include "gnunet_getopt_lib.h"
36 #include "gnunet_os_lib.h"
37 #include "gnunet_program_lib.h"
38 #include "gnunet_scheduler_lib.h"
41 #define VERBOSE GNUNET_NO
43 static char *test_phrase = "HELLO WORLD";
47 /* Pipe to write to started processes stdin (on write end) */
48 struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
49 /* Pipe to read from started processes stdout (on read end) */
50 struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
52 GNUNET_SCHEDULER_TaskIdentifier die_task;
55 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
58 if (0 != PLIBC_KILL (pid, SIGTERM))
60 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
62 GNUNET_OS_process_wait (pid);
63 GNUNET_DISK_pipe_close(hello_pipe_stdout);
64 GNUNET_DISK_pipe_close(hello_pipe_stdin);
68 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
72 memset(&buf, 0, sizeof(buf));
74 bytes = GNUNET_DISK_file_read(stdout_read_handle, &buf, sizeof(buf));
77 fprintf(stderr, "bytes is %d\n", bytes);
83 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
84 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
88 ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
90 fprintf(stderr, "read %s\n", &buf[0]);
94 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
95 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
99 GNUNET_SCHEDULER_add_read_file (tc->sched,
100 GNUNET_TIME_UNIT_FOREVER_REL,
101 stdout_read_handle, &read_call, stdout_read_handle);
107 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
115 GNUNET_asprintf(&fn, "cat");
117 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES);
118 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
120 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
127 pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
128 "test_gnunet_echo_hello", "-", NULL);
131 /* Close the write end of the read pipe */
132 GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
133 /* Close the read end of the write pipe */
134 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
135 /* Get the FD to read from */
136 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ), &fd_stdout, sizeof (int));
137 /* Get the FD to write to */
138 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), &fd_stdin, sizeof (int));
140 /* Write the test_phrase to the cat process */
141 if (write(fd_stdin, test_phrase, strlen(test_phrase) + 1) == GNUNET_SYSERR)
147 /* Close the write end to end the cycle! */
148 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
150 stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
152 die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
154 GNUNET_SCHEDULER_add_read_file (tc->sched,
155 GNUNET_TIME_UNIT_FOREVER_REL,
156 stdout_read_handle, &read_call, (void *)stdout_read_handle);
161 * Main method, starts scheduler with task1,
162 * checks that "ok" is correct at the end.
168 GNUNET_SCHEDULER_run (&task, &ok);
174 main (int argc, char *argv[])
178 GNUNET_log_setup ("test-start-process",