diff options
Diffstat (limited to 'src/util/test_os_start_process.c')
-rw-r--r-- | src/util/test_os_start_process.c | 289 |
1 files changed, 0 insertions, 289 deletions
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c deleted file mode 100644 index 435b70e1a..000000000 --- a/src/util/test_os_start_process.c +++ /dev/null | |||
@@ -1,289 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
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_util_lib.h" | ||
30 | #include "disk.h" | ||
31 | |||
32 | |||
33 | static const char *test_phrase = "HELLO WORLD"; | ||
34 | |||
35 | static int ok; | ||
36 | |||
37 | static struct GNUNET_OS_Process *proc; | ||
38 | |||
39 | /** | ||
40 | * Pipe to write to started processes stdin (on write end) | ||
41 | */ | ||
42 | static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin; | ||
43 | |||
44 | /** | ||
45 | * Pipe to read from started processes stdout (on read end) | ||
46 | */ | ||
47 | static struct GNUNET_DISK_PipeHandle *hello_pipe_stdout; | ||
48 | |||
49 | static struct GNUNET_SCHEDULER_Task *die_task; | ||
50 | |||
51 | struct read_context | ||
52 | { | ||
53 | char buf[16]; | ||
54 | int buf_offset; | ||
55 | const struct GNUNET_DISK_FileHandle *stdout_read_handle; | ||
56 | }; | ||
57 | |||
58 | |||
59 | static struct read_context rc; | ||
60 | |||
61 | |||
62 | static void | ||
63 | end_task (void *cls) | ||
64 | { | ||
65 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
66 | { | ||
67 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
68 | } | ||
69 | GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); | ||
70 | GNUNET_OS_process_destroy (proc); | ||
71 | proc = NULL; | ||
72 | GNUNET_DISK_pipe_close (hello_pipe_stdout); | ||
73 | GNUNET_DISK_pipe_close (hello_pipe_stdin); | ||
74 | } | ||
75 | |||
76 | |||
77 | static void | ||
78 | read_call (void *cls) | ||
79 | { | ||
80 | int bytes; | ||
81 | |||
82 | bytes = GNUNET_DISK_file_read (rc.stdout_read_handle, | ||
83 | &rc.buf[rc.buf_offset], | ||
84 | sizeof(rc.buf) - rc.buf_offset); | ||
85 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
86 | "bytes is %d\n", | ||
87 | bytes); | ||
88 | |||
89 | if (bytes < 1) | ||
90 | { | ||
91 | GNUNET_break (0); | ||
92 | ok = 1; | ||
93 | GNUNET_SCHEDULER_cancel (die_task); | ||
94 | (void) GNUNET_SCHEDULER_add_now (&end_task, NULL); | ||
95 | return; | ||
96 | } | ||
97 | |||
98 | ok = strncmp (rc.buf, test_phrase, strlen (test_phrase)); | ||
99 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
100 | "read %s\n", | ||
101 | &rc.buf[rc.buf_offset]); | ||
102 | rc.buf_offset += bytes; | ||
103 | |||
104 | if (0 == ok) | ||
105 | { | ||
106 | GNUNET_SCHEDULER_cancel (die_task); | ||
107 | (void) GNUNET_SCHEDULER_add_now (&end_task, NULL); | ||
108 | return; | ||
109 | } | ||
110 | |||
111 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
112 | rc.stdout_read_handle, | ||
113 | &read_call, | ||
114 | NULL); | ||
115 | } | ||
116 | |||
117 | |||
118 | static void | ||
119 | run_task (void *cls) | ||
120 | { | ||
121 | const struct GNUNET_DISK_FileHandle *stdout_read_handle; | ||
122 | const struct GNUNET_DISK_FileHandle *wh; | ||
123 | |||
124 | hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW); | ||
125 | hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW); | ||
126 | if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) | ||
127 | { | ||
128 | GNUNET_break (0); | ||
129 | ok = 1; | ||
130 | return; | ||
131 | } | ||
132 | |||
133 | proc = | ||
134 | GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ERR, | ||
135 | hello_pipe_stdin, hello_pipe_stdout, NULL, | ||
136 | "cat", | ||
137 | "cat", "-", NULL); | ||
138 | /* Close the write end of the read pipe */ | ||
139 | GNUNET_DISK_pipe_close_end (hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE); | ||
140 | /* Close the read end of the write pipe */ | ||
141 | GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ); | ||
142 | |||
143 | wh = GNUNET_DISK_pipe_handle (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); | ||
144 | |||
145 | /* Write the test_phrase to the cat process */ | ||
146 | if (GNUNET_DISK_file_write (wh, test_phrase, strlen (test_phrase) + 1) != | ||
147 | strlen (test_phrase) + 1) | ||
148 | { | ||
149 | GNUNET_break (0); | ||
150 | ok = 1; | ||
151 | return; | ||
152 | } | ||
153 | |||
154 | /* Close the write end to end the cycle! */ | ||
155 | GNUNET_DISK_pipe_close_end (hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); | ||
156 | |||
157 | stdout_read_handle = | ||
158 | GNUNET_DISK_pipe_handle (hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ); | ||
159 | |||
160 | die_task = | ||
161 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
162 | (GNUNET_TIME_UNIT_MINUTES, 1), | ||
163 | &end_task, | ||
164 | NULL); | ||
165 | |||
166 | memset (&rc, 0, sizeof(rc)); | ||
167 | rc.stdout_read_handle = stdout_read_handle; | ||
168 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
169 | stdout_read_handle, | ||
170 | &read_call, | ||
171 | NULL); | ||
172 | } | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Main method, starts scheduler with task1, | ||
177 | * checks that "ok" is correct at the end. | ||
178 | */ | ||
179 | static int | ||
180 | check_run () | ||
181 | { | ||
182 | ok = 1; | ||
183 | GNUNET_SCHEDULER_run (&run_task, &ok); | ||
184 | return ok; | ||
185 | } | ||
186 | |||
187 | |||
188 | /** | ||
189 | * Test killing via pipe. | ||
190 | */ | ||
191 | static int | ||
192 | check_kill () | ||
193 | { | ||
194 | char *fn; | ||
195 | |||
196 | hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW); | ||
197 | hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW); | ||
198 | if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) | ||
199 | { | ||
200 | return 1; | ||
201 | } | ||
202 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
203 | proc = | ||
204 | GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ERR | ||
205 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
206 | hello_pipe_stdin, | ||
207 | hello_pipe_stdout, | ||
208 | NULL, | ||
209 | fn, | ||
210 | "gnunet-service-resolver", "-", | ||
211 | NULL); | ||
212 | if (NULL == proc) | ||
213 | { | ||
214 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
215 | "Failed to launch gnunet-service-resolver. Is your system setup correct?\n"); | ||
216 | return 77; | ||
217 | } | ||
218 | sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */ | ||
219 | GNUNET_free (fn); | ||
220 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
221 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
222 | GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); | ||
223 | GNUNET_OS_process_destroy (proc); | ||
224 | proc = NULL; | ||
225 | GNUNET_DISK_pipe_close (hello_pipe_stdout); | ||
226 | GNUNET_DISK_pipe_close (hello_pipe_stdin); | ||
227 | return 0; | ||
228 | } | ||
229 | |||
230 | |||
231 | /** | ||
232 | * Test killing via pipe. | ||
233 | */ | ||
234 | static int | ||
235 | check_instant_kill () | ||
236 | { | ||
237 | char *fn; | ||
238 | |||
239 | hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW); | ||
240 | hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW); | ||
241 | if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) | ||
242 | { | ||
243 | return 1; | ||
244 | } | ||
245 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
246 | proc = | ||
247 | GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ERR | ||
248 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
249 | hello_pipe_stdin, hello_pipe_stdout, NULL, | ||
250 | fn, | ||
251 | "gnunet-service-resolver", "-", NULL); | ||
252 | if (NULL == proc) | ||
253 | { | ||
254 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
255 | "Failed to launch gnunet-service-resolver. Is your system setup correct?\n"); | ||
256 | return 77; | ||
257 | } | ||
258 | if (0 != GNUNET_OS_process_kill (proc, | ||
259 | GNUNET_TERM_SIG)) | ||
260 | { | ||
261 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
262 | } | ||
263 | GNUNET_free (fn); | ||
264 | GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); | ||
265 | GNUNET_OS_process_destroy (proc); | ||
266 | proc = NULL; | ||
267 | GNUNET_DISK_pipe_close (hello_pipe_stdout); | ||
268 | GNUNET_DISK_pipe_close (hello_pipe_stdin); | ||
269 | return 0; | ||
270 | } | ||
271 | |||
272 | |||
273 | int | ||
274 | main (int argc, char *argv[]) | ||
275 | { | ||
276 | int ret; | ||
277 | |||
278 | GNUNET_log_setup ("test-os-start-process", | ||
279 | "WARNING", | ||
280 | NULL); | ||
281 | ret = 0; | ||
282 | ret |= check_run (); | ||
283 | ret |= check_kill (); | ||
284 | ret |= check_instant_kill (); | ||
285 | return ret; | ||
286 | } | ||
287 | |||
288 | |||
289 | /* end of test_os_start_process.c */ | ||