use search path
[oweals/gnunet.git] / src / util / test_os_start_process.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file util/test_os_start_process.c
22  * @brief testcase for os start process code
23  *
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.
27  *
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?
32  */
33 #include "platform.h"
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"
39 #include "disk.h"
40
41 #define VERBOSE GNUNET_NO
42
43 static char *test_phrase = "HELLO WORLD";
44 static int ok;
45
46 pid_t pid;
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;
51
52 GNUNET_SCHEDULER_TaskIdentifier die_task;
53
54 static void
55 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
56 {
57
58   if (0 != PLIBC_KILL (pid, SIGTERM))
59     {
60       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
61     }
62   GNUNET_OS_process_wait (pid);
63   GNUNET_DISK_pipe_close(hello_pipe_stdout);
64   GNUNET_DISK_pipe_close(hello_pipe_stdin);
65 }
66
67 static void
68 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
71   char buf[16];
72   memset(&buf, 0, sizeof(buf));
73   int bytes;
74   bytes = GNUNET_DISK_file_read(stdout_read_handle, &buf, sizeof(buf));
75
76 #if VERBOSE
77   fprintf(stderr, "bytes is %d\n", bytes);
78 #endif
79
80   if (bytes < 1)
81     {
82       ok = 1;
83       GNUNET_SCHEDULER_cancel(tc->sched, die_task);
84       GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
85       return;
86     }
87
88   ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
89 #if VERBOSE
90   fprintf(stderr, "read %s\n", &buf[0]);
91 #endif
92   if (ok == 0)
93     {
94       GNUNET_SCHEDULER_cancel(tc->sched, die_task);
95       GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
96       return;
97     }
98
99   GNUNET_SCHEDULER_add_read_file (tc->sched,
100                                        GNUNET_TIME_UNIT_FOREVER_REL,
101                                        stdout_read_handle, &read_call, stdout_read_handle);
102
103 }
104
105
106 static void
107 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   char *fn;
110   int fd_stdout;
111   int fd_stdin;
112
113   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
114
115   GNUNET_asprintf(&fn, "cat");
116
117   hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES);
118   hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
119
120   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
121     {
122       ok = 1;
123       GNUNET_free (fn);
124       return;
125     }
126
127   pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
128                                  "test_gnunet_echo_hello", "-", NULL);
129   GNUNET_free (fn);
130
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));
139
140   /* Write the test_phrase to the cat process */
141   if (write(fd_stdin, test_phrase, strlen(test_phrase) + 1) == GNUNET_SYSERR)
142     {
143       ok = 1;
144       return;
145     }
146
147   /* Close the write end to end the cycle! */
148   GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
149
150   stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
151
152   die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
153
154   GNUNET_SCHEDULER_add_read_file (tc->sched,
155                                   GNUNET_TIME_UNIT_FOREVER_REL,
156                                   stdout_read_handle, &read_call, (void *)stdout_read_handle);
157
158 }
159
160 /**
161  * Main method, starts scheduler with task1,
162  * checks that "ok" is correct at the end.
163  */
164 static int
165 check ()
166 {
167   ok = 1;
168   GNUNET_SCHEDULER_run (&task, &ok);
169   return ok;
170 }
171
172
173 int
174 main (int argc, char *argv[])
175 {
176   int ret;
177
178   GNUNET_log_setup ("test-start-process",
179 #if VERBOSE
180                     "DEBUG",
181 #else
182                     "WARNING",
183 #endif
184                     NULL);
185   ret = check ();
186
187   return ret;
188 }