documentation for new parameters
[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 pid_t pid;
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 != PLIBC_KILL (pid, SIGTERM))
54     {
55       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
56     }
57   GNUNET_OS_process_wait (pid);
58   GNUNET_DISK_pipe_close(hello_pipe_stdout);
59   GNUNET_DISK_pipe_close(hello_pipe_stdin);
60 }
61
62 static void
63 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
66   char buf[16];
67   memset(&buf, 0, sizeof(buf));
68   int bytes;
69   bytes = GNUNET_DISK_file_read(stdout_read_handle, &buf, sizeof(buf));
70
71 #if VERBOSE
72   fprintf(stderr, "bytes is %d\n", bytes);
73 #endif
74
75   if (bytes < 1)
76     {
77       GNUNET_break (0);
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   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
107   const struct GNUNET_DISK_FileHandle *wh;
108 #ifndef WINDOWS
109   GNUNET_asprintf(&fn, "cat");
110 #else
111   GNUNET_asprintf(&fn, "./.libs/test_os_start_process_cat.exe");
112 #endif
113
114   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
115   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
116
117   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
118     {
119       GNUNET_break (0);
120       ok = 1;
121       GNUNET_free (fn);
122       return;
123     }
124
125   pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
126                                  "test_gnunet_echo_hello", "-", NULL);
127   GNUNET_free (fn);
128
129   /* Close the write end of the read pipe */
130   GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
131   /* Close the read end of the write pipe */
132   GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
133
134   wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
135
136   /* Write the test_phrase to the cat process */
137   if (GNUNET_DISK_file_write(wh, test_phrase, strlen(test_phrase) + 1) != strlen(test_phrase) + 1)
138     {
139       GNUNET_break (0);
140       ok = 1;
141       return;
142     }
143
144   /* Close the write end to end the cycle! */
145   GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
146
147   stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
148
149   die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
150
151   GNUNET_SCHEDULER_add_read_file (tc->sched,
152                                   GNUNET_TIME_UNIT_FOREVER_REL,
153                                   stdout_read_handle, &read_call, (void *)stdout_read_handle);
154
155 }
156
157 /**
158  * Main method, starts scheduler with task1,
159  * checks that "ok" is correct at the end.
160  */
161 static int
162 check ()
163 {
164   ok = 1;
165   GNUNET_SCHEDULER_run (&task, &ok);
166   return ok;
167 }
168
169
170 int
171 main (int argc, char *argv[])
172 {
173   int ret;
174
175   GNUNET_log_setup ("test-os-start-process",
176 #if VERBOSE
177                     "DEBUG",
178 #else
179                     "WARNING",
180 #endif
181                     NULL);
182   ret = check ();
183
184   return ret;
185 }