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));
79 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
80 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
84 ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
86 fprintf(stderr, "read %s\n", &buf[0]);
90 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
91 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
95 GNUNET_SCHEDULER_add_read_file (tc->sched,
96 GNUNET_TIME_UNIT_FOREVER_REL,
97 stdout_read_handle, &read_call, stdout_read_handle);
103 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
112 buf = GNUNET_malloc(strlen(test_phrase) + 1);
113 GNUNET_asprintf(&fn, "cat");
115 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES);
116 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
118 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
124 pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
125 "test_gnunet_echo_hello", NULL);
127 /* Close the write end of the read pipe */
128 GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
129 /* Close the read end of the write pipe */
130 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
131 /* Get the FD to read from */
132 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ), &fd_stdout, sizeof (int));
133 /* Get the FD to write to */
134 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), &fd_stdin, sizeof (int));
136 /* Write the test_phrase to the cat process */
137 if (write(fd_stdin, test_phrase, strlen(test_phrase) + 1) == GNUNET_SYSERR)
143 /* Close the write end to end the cycle! */
144 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
146 stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
148 die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
150 GNUNET_SCHEDULER_add_read_file (tc->sched,
151 GNUNET_TIME_UNIT_FOREVER_REL,
152 stdout_read_handle, &read_call, (void *)stdout_read_handle);
153 /* Read from the cat process, hopefully get the phrase we wrote to it! */
155 /*while (read(fd_stdout, buf, strlen(test_phrase) + 1) > 0)
157 ret = strncmp(buf, test_phrase, strlen(test_phrase));
163 * Main method, starts scheduler with task1,
164 * checks that "ok" is correct at the end.
170 GNUNET_SCHEDULER_run (&task, &ok);
176 main (int argc, char *argv[])
180 GNUNET_log_setup ("test-start-process",