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.
29 #include "gnunet_common.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
36 #define VERBOSE GNUNET_NO
38 static char *test_phrase = "HELLO WORLD";
42 /* Pipe to write to started processes stdin (on write end) */
43 struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
44 /* Pipe to read from started processes stdout (on read end) */
45 struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
47 GNUNET_SCHEDULER_TaskIdentifier die_task;
50 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53 if (0 != PLIBC_KILL (pid, SIGTERM))
55 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
57 GNUNET_OS_process_wait (pid);
58 GNUNET_DISK_pipe_close(hello_pipe_stdout);
59 GNUNET_DISK_pipe_close(hello_pipe_stdin);
63 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
65 struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
67 memset(&buf, 0, sizeof(buf));
69 bytes = GNUNET_DISK_file_read(stdout_read_handle, &buf, sizeof(buf));
72 fprintf(stderr, "bytes is %d\n", bytes);
78 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
79 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
83 ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
85 fprintf(stderr, "read %s\n", &buf[0]);
89 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
90 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
94 GNUNET_SCHEDULER_add_read_file (tc->sched,
95 GNUNET_TIME_UNIT_FOREVER_REL,
96 stdout_read_handle, &read_call, stdout_read_handle);
102 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
108 GNUNET_asprintf(&fn, "cat");
110 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES);
111 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
113 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
120 pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
121 "test_gnunet_echo_hello", "-", NULL);
124 /* Close the write end of the read pipe */
125 GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
126 /* Close the read end of the write pipe */
127 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
129 /* Write the test_phrase to the cat process */
130 if (GNUNET_DISK_file_write(hello_pipe_stdin, test_phrase, strlen(test_phrase) + 1) != GNUNET_YES)
136 /* Close the write end to end the cycle! */
137 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
139 stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
141 die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
143 GNUNET_SCHEDULER_add_read_file (tc->sched,
144 GNUNET_TIME_UNIT_FOREVER_REL,
145 stdout_read_handle, &read_call, (void *)stdout_read_handle);
150 * Main method, starts scheduler with task1,
151 * checks that "ok" is correct at the end.
157 GNUNET_SCHEDULER_run (&task, &ok);
163 main (int argc, char *argv[])
167 GNUNET_log_setup ("test-start-process",