1 /* Test of create_pipe_bidi/wait_subprocess.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "wait-process.h"
30 /* Depending on arguments, this test intentionally closes stderr or
31 starts life with stderr closed. So, the error messages might not
32 always print, but at least the exit status will be reliable. */
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 /* Create a bi-directional pipe to a test child, and validate that the
46 child program returns the expected output. The child is the same
47 program as the parent ARGV0, but with different arguments.
48 STDERR_CLOSED is true if we have already closed fd 2. */
50 test_pipe (const char *argv0, bool stderr_closed)
55 char buffer[2] = { 'a', 'a' };
59 argv[1] = stderr_closed ? "9" : "8";
61 pid = create_pipe_bidi (argv0, argv0, (char **) argv,
62 false, true, true, fd);
64 ASSERT (STDERR_FILENO < fd[0]);
65 ASSERT (STDERR_FILENO < fd[1]);
67 /* Push child's input. */
68 ASSERT (write (fd[1], buffer, 1) == 1);
70 /* Get child's output. */
71 ASSERT (read (fd[0], buffer, 2) == 1);
74 ASSERT (wait_subprocess (pid, argv0, true, false, true, true, NULL) == 0);
75 ASSERT (close (fd[0]) == 0);
76 ASSERT (close (fd[1]) == 0);
78 /* Check the result. */
79 ASSERT (buffer[0] == 'b');
80 ASSERT (buffer[1] == 'a');
84 main (int argc, const char *argv[])
89 test = atoi (argv[1]);
92 /* Driver cases. Selectively close various standard fds, to
93 ensure the child process is not impacted by this. */
123 /* Slave cases. Read one byte from fd 0, and write its value
124 plus one to fd 1. fd 2 should be closed iff the argument is
125 9. Check that no other fd's leaked. */
130 ASSERT (read (STDIN_FILENO, buffer, 1) == 1);
132 ASSERT (write (STDOUT_FILENO, buffer, 1) == 1);
135 /* Try to keep stderr open for better diagnostics. */
136 i = fcntl (STDERR_FILENO, F_GETFL);
138 /* But allow compilation on mingw. */
139 i = close (STDERR_FILENO);
146 ASSERT (errno == EBADF);
148 for (i = 3; i < 7; i++)
151 ASSERT (close (i) == -1);
152 ASSERT (errno == EBADF);
159 /* All remaining code is for the driver. Plug any leaks inherited
160 from outside world before starting, so that child has a clean
161 slate (at least for the fds that we might be manipulating). */
162 for (i = 3; i < 7; i++)
164 test_pipe (argv[0], 3 < test);