fixing doxygen warnings
[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 (bytes < 1)
77     {
78       ok = 1;
79       GNUNET_SCHEDULER_cancel(tc->sched, die_task);
80       GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
81       return;
82     }
83
84   ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
85 #if VERBOSE
86   fprintf(stderr, "read %s\n", &buf[0]);
87 #endif
88   if (ok == 0)
89     {
90       GNUNET_SCHEDULER_cancel(tc->sched, die_task);
91       GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
92       return;
93     }
94
95   GNUNET_SCHEDULER_add_read_file (tc->sched,
96                                        GNUNET_TIME_UNIT_FOREVER_REL,
97                                        stdout_read_handle, &read_call, stdout_read_handle);
98
99 }
100
101
102 static void
103 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   char *fn;
106   char *buf;
107   int fd_stdout;
108   int fd_stdin;
109
110   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
111
112   buf = GNUNET_malloc(strlen(test_phrase) + 1);
113   GNUNET_asprintf(&fn, "cat");
114
115   hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES);
116   hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
117
118   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
119     {
120       ok = 1;
121       return;
122     }
123
124   pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
125                                  "test_gnunet_echo_hello", NULL);
126
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));
135
136   /* Write the test_phrase to the cat process */
137   if (write(fd_stdin, test_phrase, strlen(test_phrase) + 1) == GNUNET_SYSERR)
138     {
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   /* Read from the cat process, hopefully get the phrase we wrote to it! */
154
155   /*while (read(fd_stdout, buf, strlen(test_phrase) + 1) > 0)
156     {
157       ret = strncmp(buf, test_phrase, strlen(test_phrase));
158     }
159   */
160 }
161
162 /**
163  * Main method, starts scheduler with task1,
164  * checks that "ok" is correct at the end.
165  */
166 static int
167 check ()
168 {
169   ok = 1;
170   GNUNET_SCHEDULER_run (&task, &ok);
171   return ok;
172 }
173
174
175 int
176 main (int argc, char *argv[])
177 {
178   int ret;
179
180   GNUNET_log_setup ("test-start-process",
181 #if VERBOSE
182                     "DEBUG",
183 #else
184                     "WARNING",
185 #endif
186                     NULL);
187   ret = check ();
188
189   return ret;
190 }