bce1c71516ca614a48d739de9d31f08c30085d26
[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 3, 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 #include "platform.h"
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"
34 #include "disk.h"
35
36 #define VERBOSE GNUNET_NO
37
38 static char *test_phrase = "HELLO WORLD";
39 static int ok;
40
41 static struct GNUNET_OS_Process *proc;
42 /* Pipe to write to started processes stdin (on write end) */
43 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
44 /* Pipe to read from started processes stdout (on read end) */
45 static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
46
47 static GNUNET_SCHEDULER_TaskIdentifier die_task;
48
49 static void
50 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52
53   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
54     {
55       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
56     }
57   GNUNET_OS_process_wait (proc);
58   GNUNET_OS_process_close (proc);
59   proc = NULL;
60   GNUNET_DISK_pipe_close(hello_pipe_stdout);
61   GNUNET_DISK_pipe_close(hello_pipe_stdin);
62 }
63
64 static void
65 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
66 {
67   struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
68   char buf[16];
69   memset(&buf, 0, sizeof(buf));
70   int bytes;
71   bytes = GNUNET_DISK_file_read(stdout_read_handle, &buf, sizeof(buf));
72
73 #if VERBOSE
74   fprintf(stderr, "bytes is %d\n", bytes);
75 #endif
76
77   if (bytes < 1)
78     {
79       GNUNET_break (0);
80       ok = 1;
81       GNUNET_SCHEDULER_cancel(tc->sched, die_task);
82       GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
83       return;
84     }
85
86   ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
87 #if VERBOSE
88   fprintf(stderr, "read %s\n", &buf[0]);
89 #endif
90   if (ok == 0)
91     {
92       GNUNET_SCHEDULER_cancel(tc->sched, die_task);
93       GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
94       return;
95     }
96
97   GNUNET_SCHEDULER_add_read_file (tc->sched,
98                                        GNUNET_TIME_UNIT_FOREVER_REL,
99                                        stdout_read_handle, &read_call, stdout_read_handle);
100
101 }
102
103
104 static void
105 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 {
107   char *fn;
108   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
109   const struct GNUNET_DISK_FileHandle *wh;
110
111   GNUNET_asprintf(&fn, "cat");
112
113   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
114   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
115
116   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
117     {
118       GNUNET_break (0);
119       ok = 1;
120       GNUNET_free (fn);
121       return;
122     }
123
124   proc = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
125                                  "test_gnunet_echo_hello", "-", NULL);
126   GNUNET_free (fn);
127
128   /* Close the write end of the read pipe */
129   GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
130   /* Close the read end of the write pipe */
131   GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
132
133   wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
134
135   /* Write the test_phrase to the cat process */
136   if (GNUNET_DISK_file_write(wh, test_phrase, strlen(test_phrase) + 1) != strlen(test_phrase) + 1)
137     {
138       GNUNET_break (0);
139       ok = 1;
140       return;
141     }
142
143   /* Close the write end to end the cycle! */
144   GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
145
146   stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
147
148   die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
149
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
154 }
155
156 /**
157  * Main method, starts scheduler with task1,
158  * checks that "ok" is correct at the end.
159  */
160 static int
161 check ()
162 {
163   ok = 1;
164   GNUNET_SCHEDULER_run (&task, &ok);
165   return ok;
166 }
167
168
169 int
170 main (int argc, char *argv[])
171 {
172   int ret;
173
174   GNUNET_log_setup ("test-os-start-process",
175 #if VERBOSE
176                     "DEBUG",
177 #else
178                     "WARNING",
179 #endif
180                     NULL);
181   ret = check ();
182
183   return ret;
184 }