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