]> git.ipfire.org Git - thirdparty/glibc.git/blob - support/tst-support_capture_subprocess.c
manual: Adjust twalk_r documentation.
[thirdparty/glibc.git] / support / tst-support_capture_subprocess.c
1 /* Test capturing output from a subprocess.
2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <support/capture_subprocess.h>
24 #include <support/check.h>
25 #include <support/support.h>
26 #include <support/temp_file.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #include <paths.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include <array_length.h>
34
35 /* Nonzero if the program gets called via 'exec'. */
36 static int restart;
37
38 /* Hold the four initial argument used to respawn the process. */
39 static char *initial_argv[5];
40
41 /* Write one byte at *P to FD and advance *P. Do nothing if *P is
42 '\0'. */
43 static void
44 transfer (const unsigned char **p, int fd)
45 {
46 if (**p != '\0')
47 {
48 TEST_VERIFY (write (fd, *p, 1) == 1);
49 ++*p;
50 }
51 }
52
53 /* Determine the order in which stdout and stderr are written. */
54 enum write_mode { out_first, err_first, interleave,
55 write_mode_last = interleave };
56
57 static const char *
58 write_mode_to_str (enum write_mode mode)
59 {
60 switch (mode)
61 {
62 case out_first: return "out_first";
63 case err_first: return "err_first";
64 case interleave: return "interleave";
65 default: return "write_mode_last";
66 }
67 }
68
69 static enum write_mode
70 str_to_write_mode (const char *mode)
71 {
72 if (strcmp (mode, "out_first") == 0)
73 return out_first;
74 else if (strcmp (mode, "err_first") == 0)
75 return err_first;
76 else if (strcmp (mode, "interleave") == 0)
77 return interleave;
78 return write_mode_last;
79 }
80
81 /* Describe what to write in the subprocess. */
82 struct test
83 {
84 char *out;
85 char *err;
86 enum write_mode write_mode;
87 int signal;
88 int status;
89 };
90
91 _Noreturn static void
92 test_common (const struct test *test)
93 {
94 bool mode_ok = false;
95 switch (test->write_mode)
96 {
97 case out_first:
98 TEST_VERIFY (fputs (test->out, stdout) >= 0);
99 TEST_VERIFY (fflush (stdout) == 0);
100 TEST_VERIFY (fputs (test->err, stderr) >= 0);
101 TEST_VERIFY (fflush (stderr) == 0);
102 mode_ok = true;
103 break;
104 case err_first:
105 TEST_VERIFY (fputs (test->err, stderr) >= 0);
106 TEST_VERIFY (fflush (stderr) == 0);
107 TEST_VERIFY (fputs (test->out, stdout) >= 0);
108 TEST_VERIFY (fflush (stdout) == 0);
109 mode_ok = true;
110 break;
111 case interleave:
112 {
113 const unsigned char *pout = (const unsigned char *) test->out;
114 const unsigned char *perr = (const unsigned char *) test->err;
115 do
116 {
117 transfer (&pout, STDOUT_FILENO);
118 transfer (&perr, STDERR_FILENO);
119 }
120 while (*pout != '\0' || *perr != '\0');
121 }
122 mode_ok = true;
123 break;
124 }
125 TEST_VERIFY (mode_ok);
126
127 if (test->signal != 0)
128 raise (test->signal);
129 exit (test->status);
130 }
131
132 static int
133 parse_int (const char *str)
134 {
135 char *endptr;
136 long int ret = strtol (str, &endptr, 10);
137 TEST_COMPARE (errno, 0);
138 TEST_VERIFY (ret >= 0 && ret <= INT_MAX);
139 return ret;
140 }
141
142 /* For use with support_capture_subprogram. */
143 _Noreturn static void
144 handle_restart (char *out, char *err, const char *write_mode,
145 const char *signal, const char *status)
146 {
147 struct test test =
148 {
149 out,
150 err,
151 str_to_write_mode (write_mode),
152 parse_int (signal),
153 parse_int (status)
154 };
155 test_common (&test);
156 }
157
158 /* For use with support_capture_subprocess. */
159 _Noreturn static void
160 callback (void *closure)
161 {
162 const struct test *test = closure;
163 test_common (test);
164 }
165
166 /* Create a heap-allocated random string of letters. */
167 static char *
168 random_string (size_t length)
169 {
170 char *result = xmalloc (length + 1);
171 for (size_t i = 0; i < length; ++i)
172 result[i] = 'a' + (rand () % 26);
173 result[length] = '\0';
174 return result;
175 }
176
177 /* Check that the specific stream from the captured subprocess matches
178 expectations. */
179 static void
180 check_stream (const char *what, const struct xmemstream *stream,
181 const char *expected)
182 {
183 if (strcmp (stream->buffer, expected) != 0)
184 {
185 support_record_failure ();
186 printf ("error: captured %s data incorrect\n"
187 " expected: %s\n"
188 " actual: %s\n",
189 what, expected, stream->buffer);
190 }
191 if (stream->length != strlen (expected))
192 {
193 support_record_failure ();
194 printf ("error: captured %s data length incorrect\n"
195 " expected: %zu\n"
196 " actual: %zu\n",
197 what, strlen (expected), stream->length);
198 }
199 }
200
201 static struct support_capture_subprocess
202 do_subprocess (struct test *test)
203 {
204 return support_capture_subprocess (callback, test);
205 }
206
207 static struct support_capture_subprocess
208 do_subprogram (const struct test *test)
209 {
210 /* Three digits per byte plus null terminator. */
211 char signalstr[3 * sizeof(int) + 1];
212 snprintf (signalstr, sizeof (signalstr), "%d", test->signal);
213 char statusstr[3 * sizeof(int) + 1];
214 snprintf (statusstr, sizeof (statusstr), "%d", test->status);
215
216 int argc = 0;
217 enum {
218 /* 4 elements from initial_argv (path to ld.so, '--library-path', the
219 path', and application name'), 2 for restart argument ('--direct',
220 '--restart'), 5 arguments plus NULL. */
221 argv_size = 12
222 };
223 char *args[argv_size];
224
225 for (char **arg = initial_argv; *arg != NULL; arg++)
226 args[argc++] = *arg;
227
228 args[argc++] = (char*) "--direct";
229 args[argc++] = (char*) "--restart";
230
231 args[argc++] = test->out;
232 args[argc++] = test->err;
233 args[argc++] = (char*) write_mode_to_str (test->write_mode);
234 args[argc++] = signalstr;
235 args[argc++] = statusstr;
236 args[argc] = NULL;
237 TEST_VERIFY (argc < argv_size);
238
239 return support_capture_subprogram (args[0], args);
240 }
241
242 enum test_type
243 {
244 subprocess,
245 subprogram,
246 };
247
248 static int
249 do_multiple_tests (enum test_type type)
250 {
251 const int lengths[] = {0, 1, 17, 512, 20000, -1};
252
253 /* Test multiple combinations of support_capture_sub{process,program}.
254
255 length_idx_stdout: Index into the lengths array above,
256 controls how many bytes are written by the subprocess to
257 standard output.
258 length_idx_stderr: Same for standard error.
259 write_mode: How standard output and standard error writes are
260 ordered.
261 signal: Exit with no signal if zero, with SIGTERM if one.
262 status: Process exit status: 0 if zero, 3 if one. */
263 for (int length_idx_stdout = 0; lengths[length_idx_stdout] >= 0;
264 ++length_idx_stdout)
265 for (int length_idx_stderr = 0; lengths[length_idx_stderr] >= 0;
266 ++length_idx_stderr)
267 for (int write_mode = 0; write_mode < write_mode_last; ++write_mode)
268 for (int signal = 0; signal < 2; ++signal)
269 for (int status = 0; status < 2; ++status)
270 {
271 struct test test =
272 {
273 .out = random_string (lengths[length_idx_stdout]),
274 .err = random_string (lengths[length_idx_stderr]),
275 .write_mode = write_mode,
276 .signal = signal * SIGTERM, /* 0 or SIGTERM. */
277 .status = status * 3, /* 0 or 3. */
278 };
279 TEST_VERIFY (strlen (test.out) == lengths[length_idx_stdout]);
280 TEST_VERIFY (strlen (test.err) == lengths[length_idx_stderr]);
281
282 struct support_capture_subprocess result
283 = type == subprocess ? do_subprocess (&test)
284 : do_subprogram (&test);
285
286 check_stream ("stdout", &result.out, test.out);
287 check_stream ("stderr", &result.err, test.err);
288
289 /* Allowed output for support_capture_subprocess_check. */
290 int check_allow = 0;
291 if (lengths[length_idx_stdout] > 0)
292 check_allow |= sc_allow_stdout;
293 if (lengths[length_idx_stderr] > 0)
294 check_allow |= sc_allow_stderr;
295 if (check_allow == 0)
296 check_allow = sc_allow_none;
297
298 if (test.signal != 0)
299 {
300 TEST_VERIFY (WIFSIGNALED (result.status));
301 TEST_VERIFY (WTERMSIG (result.status) == test.signal);
302 support_capture_subprocess_check (&result, "signal",
303 -SIGTERM, check_allow);
304 }
305 else
306 {
307 TEST_VERIFY (WIFEXITED (result.status));
308 TEST_VERIFY (WEXITSTATUS (result.status) == test.status);
309 support_capture_subprocess_check (&result, "exit",
310 test.status, check_allow);
311 }
312 support_capture_subprocess_free (&result);
313 free (test.out);
314 free (test.err);
315 }
316 return 0;
317 }
318
319 static int
320 do_test (int argc, char *argv[])
321 {
322 /* We must have either:
323
324 - one or four parameters if called initially:
325 + argv[1]: path for ld.so optional
326 + argv[2]: "--library-path" optional
327 + argv[3]: the library path optional
328 + argv[4]: the application name
329
330 - six parameters left if called through re-execution:
331 + argv[1]: the application name
332 + argv[2]: the stdout to print
333 + argv[3]: the stderr to print
334 + argv[4]: the write mode to use
335 + argv[5]: the signal to issue
336 + argv[6]: the exit status code to use
337
338 * When built with --enable-hardcoded-path-in-tests or issued without
339 using the loader directly.
340 */
341
342 if (argc != (restart ? 6 : 5) && argc != (restart ? 6 : 2))
343 FAIL_EXIT1 ("wrong number of arguments (%d)", argc);
344
345 if (restart)
346 {
347 handle_restart (argv[1], /* stdout */
348 argv[2], /* stderr */
349 argv[3], /* write_mode */
350 argv[4], /* signal */
351 argv[5]); /* status */
352 }
353
354 initial_argv[0] = argv[1]; /* path for ld.so */
355 initial_argv[1] = argv[2]; /* "--library-path" */
356 initial_argv[2] = argv[3]; /* the library path */
357 initial_argv[3] = argv[4]; /* the application name */
358 initial_argv[4] = NULL;
359
360 do_multiple_tests (subprocess);
361 do_multiple_tests (subprogram);
362
363 return 0;
364 }
365
366 #define CMDLINE_OPTIONS \
367 { "restart", no_argument, &restart, 1 },
368 #define TEST_FUNCTION_ARGV do_test
369 #include <support/test-driver.c>